Chapter 8: Change
8.16. 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.

We can also use random conditions:

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

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

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

If we make a new kind of value, we can also take random values from it:

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

The value "a random cloud pattern" then produces a random choice from the seven cloud patterns, each equally likely.

Testing IF which makes random choices can be rather frustrating, because a problem showing up on one attempt may not show up on another. To ease testing, 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.


118
* 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
119
* 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
    begin;
        say "[line break][The noun] expires, and is immediately carried away by the Arena slaves!";
        remove the noun from play;
        end the game in victory;
        stop the action;
    end if;
    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
    begin;
        say "[line break]You expire!";
        end the game in death;
    end if.

This last bit is a refinement to help the player keep track of how the contest is going:

When play begins:
    change the left hand status line to "You: [current hit points of player]";
    change the right hand status line to "Gladiator: [current hit points of gladiator]".

Test me with "hit gladiator / g / g / g".

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

RB
121
*** Example  Uptown Girls
A stream of random pedestrians who go by the player.

RB


PreviousContentsNext