8.18. Randomness

| Chapter 8: Change |

| 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 game is played within Inform, but (unlike the rule above) has no effect on the story file once released.

129
| Example Do 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. |
RB

130
| Example Lanista 1 Very simple randomized combat in which characters hit one another for a randomized amount of damage. |
RB

"Lanista, Part One" The Arena is a room. "Sand, blood, iron. These festivals are normally held on hot days, but the sun has gone behind a cloud and fat drops of rain now and then spatter the arena floor." The gladiator is a man in the Arena. "A bare-chested Scythian gladiator faces you, wielding a trident." We start by recording, for each person, a maximum number of points of damage the person can sustain when starting from health, and the current number of points remaining. In the tradition of role-playing games, these are referred to as hit points. A person has a number called maximum hit points. A person has a number called current hit points. The maximum hit points of the player is 35. The maximum hit points of the gladiator is 25. The current hit points of the player is 35. The current hit points of the gladiator is 25. Now our rule for the actual attack. We want first to calculate how much damage the player's attack does, inflict that damage, and remove the enemy if he's dead; then, if he doesn't die, the enemy counter-attacks, also for a randomized amount of damage, and if this kills the player, the game ends in defeat. Instead of attacking someone: let the damage be a random number between 2 and 10; say "You attack [the noun], causing [damage] points of damage!"; decrease the current hit points of the noun by the damage; if the current hit points of the noun is less than 0: say "[line break][The noun] expires, and is immediately carried away by the Arena slaves!"; remove the noun from play; end the story finally; stop the action; let the enemy damage be a random number between 2 and 10; say "[line break][The noun] attacks you, causing [enemy damage] points of damage!"; decrease the current hit points of the player by the enemy damage; if the current hit points of the player is less than 0: say "[line break]You expire!"; end the story. This last bit is a refinement to help the player keep track of how the contest is going: When play begins: now the left hand status line is "You: [current hit points of player]"; now the right hand status line is "Gladiator: [current hit points of gladiator]". Test me with "hit gladiator / g / g / g".

131
| Example Weathering The automatic weather station atop Mt. Pisgah shows randomly fluctuating temperature, pressure and cloud cover. |
RB

132
| Example Uptown Girls A stream of random pedestrians who go by the player. |
RB

Previous | Contents | Next