§8.18. Randomness

Sometimes we want to introduce random behaviour into play. We usually do this by generating random values, and then acting differently depending on what they are. The following:

a random number from 2 to 5

produces, as it suggests, a random number drawn from the choices 2, 3, 4 or 5, each of which is equally likely to come up. In fact, this isn't limited to numbers:

a random (name of kind) between (arithmetic value) and (arithmetic value) ... value


or:   

a random (name of kind) from (arithmetic value) to (arithmetic value) ... value


or:   

a random (name of kind) between (enumerated value) and (enumerated value) ... value


or:   

a random (name of kind) from (enumerated value) to (enumerated value) ... value

This phrase produces a uniformly random value in the range given. Examples:

a random number from 10 to 99
a random time from 2:31 PM to 2:57 PM

If we make a new kind of value:

A cloud pattern is a kind of value. The cloud patterns are cumulus, altocumulus, cumulonimbus, stratus, cirrus, nimbus, nimbostratus.

then we can also take random values from it:

a random cloud pattern between stratus and nimbus

which has three possible outcomes, all equally likely.

We can also use random conditions:

if a random chance of (number) in (number) succeeds:

This condition is true X/Yths of the time, where X and Y are the numbers. Example:

if a random chance of 2 in 3 succeeds, ...

Here is a rule which applies only 15% of the time:

Instead of waiting when a random chance of 15 in 100 succeeds: ...

Testing IF which makes random choices can be rather frustrating, because a problem showing up on one attempt may not show up on another. We can get around this by making use of the fact that computers do not actually generate true randomness, but instead make a sequence of apparently random numbers by applying a complicated formula to each one in order to make the next. The starting point is a number called the "seed", because the next choice grows out of it.

seed the random-number generator with (number)

This phrase changes the seed number as specified. Any random numbers generated after that depend only on the seed. Example: the following sentence will "fix" the process of generating these random numbers so that they are not random at all - the same sequence of random numbers will be produced on each run.

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

The seed value "1234" can be anything positive; a different sequence of random numbers will be produced for each different seed value. A seed value of 0 restores the RNG to properly random behaviour again.

Alternatively, it's possible the "fix" the RNG by clicking the "Make random outcomes predictable when testing" option on the Settings panel. This makes the behaviour predictable whenever the story is played within Inform, but (unlike the rule above) has no effect on the story file once released.


arrow-up.pngStart of Chapter 8: Change
arrow-left.pngBack to §8.17. Looking at containment by hand
arrow-right.pngOnward to §8.19. Random choices of things

*ExampleLanista 1
Very simple randomized combat in which characters hit one another for a randomized amount of damage.

paste.png "Do Pass Go"

Go is a room. "A giant square area, where you and your other pewter ornament friends gather before setting out to purchase London."

The pair of dice is carried by the player.

The pair of dice has a number called first die. The pair of dice has a number called second die. The first die of the pair is 6. The second die of the pair is 6. Rule for printing the name of the pair of dice while taking inventory: say "pair of dice".
Rule for printing the name of the pair of dice: say "pair of dice showing [first die of the pair plus second die of the pair]".

To say detailed state of the dice:
    if the first die of the pair is the second die of the pair, say "double [first die of the pair]";
    otherwise say "[first die of the pair] and [second die of the pair]".

The description of the pair of dice is "The pair of dice are [if the dice are carried]itching to be rolled[otherwise]showing [detailed state of the dice][end if]."

Rolling is an action applying to one carried thing. Understand "roll [something preferably held]" as rolling.

Check rolling when the noun is not the pair of dice: say "Not something you can roll." instead.
Carry out rolling:
    now the pair of dice is in the holder of the actor;
    now the first die of the pair of dice is a random number from 1 to 6;
    now the second die of the pair of dice is a random number from 1 to 6.
Report rolling:
    say "You roll [detailed state of the dice]."

Test me with "i / roll dice / look / x dice / get dice / x dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice".

Because we remember the states of the individual dice, not just a total, we can make use of the combination rolled.

The doubles count is a number that varies.
After rolling:
    if the first die of the pair is the second die of the pair, increment the doubles count;
    otherwise now the doubles count is 0;
    continue the action.

Jail is a room. "This is Jail, and not the Just Visiting periphery, either."

Every turn when the doubles count is 3:
    say "The blue-uniformed policemen blows his whistle and beckons you sternly...";
    now the player carries the pair of dice;
    now the player is in Jail;
    now the doubles count is 0.

Every turn when the doubles count is 1 and the player is in Jail:
    say "The warden gruffly releases you.";
    now the player carries the pair of dice;
    now the player is in Go.

*ExampleDo Pass Go
A pair of dice which can be rolled, and are described with their current total when not carried, and have individual scores when examined.

paste.png "Do Pass Go"

Go is a room. "A giant square area, where you and your other pewter ornament friends gather before setting out to purchase London."

The pair of dice is carried by the player.

The pair of dice has a number called first die. The pair of dice has a number called second die. The first die of the pair is 6. The second die of the pair is 6. Rule for printing the name of the pair of dice while taking inventory: say "pair of dice".
Rule for printing the name of the pair of dice: say "pair of dice showing [first die of the pair plus second die of the pair]".

To say detailed state of the dice:
    if the first die of the pair is the second die of the pair, say "double [first die of the pair]";
    otherwise say "[first die of the pair] and [second die of the pair]".

The description of the pair of dice is "The pair of dice are [if the dice are carried]itching to be rolled[otherwise]showing [detailed state of the dice][end if]."

Rolling is an action applying to one carried thing. Understand "roll [something preferably held]" as rolling.

Check rolling when the noun is not the pair of dice: say "Not something you can roll." instead.
Carry out rolling:
    now the pair of dice is in the holder of the actor;
    now the first die of the pair of dice is a random number from 1 to 6;
    now the second die of the pair of dice is a random number from 1 to 6.
Report rolling:
    say "You roll [detailed state of the dice]."

Test me with "i / roll dice / look / x dice / get dice / x dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice".

Because we remember the states of the individual dice, not just a total, we can make use of the combination rolled.

The doubles count is a number that varies.
After rolling:
    if the first die of the pair is the second die of the pair, increment the doubles count;
    otherwise now the doubles count is 0;
    continue the action.

Jail is a room. "This is Jail, and not the Just Visiting periphery, either."

Every turn when the doubles count is 3:
    say "The blue-uniformed policemen blows his whistle and beckons you sternly...";
    now the player carries the pair of dice;
    now the player is in Jail;
    now the doubles count is 0.

Every turn when the doubles count is 1 and the player is in Jail:
    say "The warden gruffly releases you.";
    now the player carries the pair of dice;
    now the player is in Go.

paste.png "Do Pass Go"

Go is a room. "A giant square area, where you and your other pewter ornament friends gather before setting out to purchase London."

The pair of dice is carried by the player.

The pair of dice has a number called first die. The pair of dice has a number called second die. The first die of the pair is 6. The second die of the pair is 6. Rule for printing the name of the pair of dice while taking inventory: say "pair of dice".
Rule for printing the name of the pair of dice: say "pair of dice showing [first die of the pair plus second die of the pair]".

To say detailed state of the dice:
    if the first die of the pair is the second die of the pair, say "double [first die of the pair]";
    otherwise say "[first die of the pair] and [second die of the pair]".

The description of the pair of dice is "The pair of dice are [if the dice are carried]itching to be rolled[otherwise]showing [detailed state of the dice][end if]."

Rolling is an action applying to one carried thing. Understand "roll [something preferably held]" as rolling.

Check rolling when the noun is not the pair of dice: say "Not something you can roll." instead.
Carry out rolling:
    now the pair of dice is in the holder of the actor;
    now the first die of the pair of dice is a random number from 1 to 6;
    now the second die of the pair of dice is a random number from 1 to 6.
Report rolling:
    say "You roll [detailed state of the dice]."

Test me with "i / roll dice / look / x dice / get dice / x dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice / roll dice".

Because we remember the states of the individual dice, not just a total, we can make use of the combination rolled.

The doubles count is a number that varies.
After rolling:
    if the first die of the pair is the second die of the pair, increment the doubles count;
    otherwise now the doubles count is 0;
    continue the action.

Jail is a room. "This is Jail, and not the Just Visiting periphery, either."

Every turn when the doubles count is 3:
    say "The blue-uniformed policemen blows his whistle and beckons you sternly...";
    now the player carries the pair of dice;
    now the player is in Jail;
    now the doubles count is 0.

Every turn when the doubles count is 1 and the player is in Jail:
    say "The warden gruffly releases you.";
    now the player carries the pair of dice;
    now the player is in Go.

*ExampleWeathering
The automatic weather station atop Mt. Pisgah shows randomly fluctuating temperature, pressure and cloud cover.

***ExampleUptown Girls
A stream of random pedestrians who go by the player.