Chapter 8: Change
8.19. Random choices of things

Writing "a random number" is not allowed, because the possible range is too large, but that was the only reason why not.

a/-- random (description of values) ... value

This phrase makes a uniformly random choice from values satisfying the description given. Example:

a random visited room
a random scene

A problem message is issued if the range is too large (for instance, "a random text"). Unexpected results may follow if no value fits the description, unless we are describing objects, in which case the result is the special value "nothing".

For instance:

say "You can see [number of adjacent rooms] way[s] from here; how about [random adjacent room]?"

But it's important to worry about the possibility that nothing qualifies - here, that no adjacent rooms exist. The above would then say:

You can see 0 ways from here; how about nothing?


133
* Example  Candy
One of several identical candies chosen at the start of play to be poisonous.

RB
134
* Example  Zork II
A "Carousel Room", as in Zork II, where moving in any direction from the room leads (at random) to one of the eight rooms nearby.

RB

All we need to do is select the player's destination for him at random:

"Zork II"

The Carousel Room is a room.

Instead of going from the Carousel Room:
    move the player to a random adjacent room.

To avoid infringing the original game too much, let's try a somewhat different setting:

The Games of Chance is north of the Carousel Room. The Haunted Funhouse is northwest of the Carousel Room. The Ferris Wheel is east of the Carousel Room. The Topsy-Turvy is northeast of the Carousel Room. The Reproduction Henge is south of the Carousel Room. The Women's Toilet is southwest of the Carousel Room. The Men's Toilet is southeast of the Carousel Room. The Cotton Candy Shop is west of the Carousel Room.

Test me with "s".

And the following means that the test runs consistently even though the numbers are theoretically random. To make them truly random, remove this line.

When play begins, seed the random-number generator with 1234.

Or if we want to add the refinement that the Carousel Room can be switched off:

"Zork II"

The Carousel Room is a room. The spinning machine is a switched on device in the Carousel Room.

And then

Instead of going from the Carousel Room when the spinning machine is switched on:
    move the player to a random adjacent room.

The Games of Chance is north of the Carousel Room. The Haunted Funhouse is northwest of the Carousel Room. The Ferris Wheel is east of the Carousel Room. The Topsy-Turvy is northeast of the Carousel Room. The Reproduction Henge is south of the Carousel Room. The Women's Toilet is southwest of the Carousel Room. The Men's Toilet is southeast of the Carousel Room. The Cotton Candy Shop is west of the Carousel Room.

Test me with "turn off machine / s / n / turn on machine / s".

When play begins, seed the random-number generator with 1234.


PreviousContentsNext