§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.


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.

*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.

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

Suppose we have an urban space we want to populate with random passers-by. These should have a range of characteristics and not always be described in the same way; and once the player has noticed one, he should be able to look at her further, until another pedestrian crosses his path.

paste.png "Uptown Girls"

Riverside Drive is a room. "There's a pleasant late-afternoon view of the Hudson, and a snap in the air, and you would rather be here than anywhere."

Instead of going a direction, say "Oh, you know where you're going; no need to deviate from the usual path."

Instead of waiting, say "You stroll along enjoying the November crispness."

It gets a little annoying to have a random event occurring every single turn of play, so let's introduce some randomness to determine how often the message appears:

Every turn when a random chance of 1 in 3 succeeds:
    reset passerby;
    choose a random row in the Table of Atmospheric Events;
    say "[event entry][paragraph break]"

Table of Atmospheric Events
event
"Slowly [a passerby] strolls by, turning to look at you as she passes."
"Some [passerby] nearly bumps into you."
"You dodge to avoid [a passerby]."
"You weave around [a passerby], who has stalled to look into a window."
"There's a ruckus as one of the ubiquitous taxis nearly collides with [a passerby] crossing the street."
"[The passerby] beside you waves to a friend across the street."
"To your left, [a passerby] drops her purse, and swears as she retrieves it."

Hair color is a kind of value. A person has hair color. the hair colors are red-headed, brunette, blonde.

Height is a kind of value. A person has height. The heights are tall, medium-height, short.

Grooming is a kind of value. A person has grooming. The groomings are messy and tidy.

To reset passerby:
    now the hair color of the passerby is a random hair color;
    now the height of the passerby is a random height;
    now the grooming of the passerby is a random grooming.

The passerby is a woman in Riverside Drive. The passerby is scenery. Understand "woman" or "lady" as the passerby. The printed name of the passerby is "[one of]woman[or]lady[purely at random]".

At this point we borrow some techniques from later to describe the woman with a random combination of characteristics, and to let the player refer to her by those traits:

Before printing the name of the passerby:
    if a random chance of 1 in 4 succeeds, say "[height] ";
    if a random chance of 1 in 4 succeeds, say "[grooming] ";
    if a random chance of 1 in 4 succeeds, say "[hair color] ".

Understand the hair color property as describing the passerby. Understand the height property as describing the passerby. Understand the grooming property as describing the passerby.

If we also wanted each of those combinations to mean some more specifically-described woman:

Instead of examining a passerby:
    repeat through Table of Passerby Descriptions:
        if hair entry is the hair color of the passerby and height entry is the height of the passerby and grooming entry is the grooming of the passerby, say "[description entry][paragraph break]".

Table of Passerby Descriptions

hair

height

grooming

description

red-headed

tall

messy

"An older woman with long red hippie-hair poking out of a ponytail in straggles, and bent to hide how tall she is."

red-headed

medium-height

messy

"A shaggy red-head with shingled hair."

red-headed

short

messy

"Almost an urchin, and very young, with ginger hair and a smudged nose and far too many freckles."

red-headed

tall

tidy

"A precise career woman with henna-red hair."

red-headed

medium-height

tidy

"Her hair is red in the way that lollipops and fire trucks are red: not by nature but by art. The rest of her clothing is pretty ordinary, though."

red-headed

short

tidy

"Thin and small in every sense, with chin-length red-hair. Even high heels do not bring her head much above your shoulder."

brunette

tall

messy

"A Juno-esque woman with dark hair, wearing something resembling a tent."

brunette

medium-height

messy

"An unremarkable woman with dark brown hair and the aura of needing a wash."

brunette

short

messy

"There are mustard stains on the t-shirt of this short brown-haired woman. Estimated age ca. 40. Possibly homeless."

brunette

tall

tidy

"A leggy brunette in business attire."

brunette

medium-height

tidy

"Medium-height, brown-haired, generally nondescript."

brunette

short

tidy

"A neat little dark-haired girl."

blonde

tall

messy

"A tall blonde of about thirteen who looks as though she has not yet figured out how to get her wardrobe to catch up with her rate of growth. Her t-shirt and her pants are too short."

blonde

medium-height

messy

"Black leather pants and the wall-o-hair look."

blonde

short

messy

"One of those shocking platinum blonde types, with a tiger-patterned skirt. Reeally trashy."

blonde

tall

tidy

"Elfin and severe, with perfectly straight hair falling to the middle of the back."

blonde

medium-height

tidy

"A rounded, Marilyn-esque blonde."

blonde

short

tidy

"Pin-precise in a blue-and-white striped suit and a boyish haircut."

Test me with "z / z / x passerby / z / z / x passerby".

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

Suppose we have an urban space we want to populate with random passers-by. These should have a range of characteristics and not always be described in the same way; and once the player has noticed one, he should be able to look at her further, until another pedestrian crosses his path.

paste.png "Uptown Girls"

Riverside Drive is a room. "There's a pleasant late-afternoon view of the Hudson, and a snap in the air, and you would rather be here than anywhere."

Instead of going a direction, say "Oh, you know where you're going; no need to deviate from the usual path."

Instead of waiting, say "You stroll along enjoying the November crispness."

It gets a little annoying to have a random event occurring every single turn of play, so let's introduce some randomness to determine how often the message appears:

Every turn when a random chance of 1 in 3 succeeds:
    reset passerby;
    choose a random row in the Table of Atmospheric Events;
    say "[event entry][paragraph break]"

Table of Atmospheric Events
event
"Slowly [a passerby] strolls by, turning to look at you as she passes."
"Some [passerby] nearly bumps into you."
"You dodge to avoid [a passerby]."
"You weave around [a passerby], who has stalled to look into a window."
"There's a ruckus as one of the ubiquitous taxis nearly collides with [a passerby] crossing the street."
"[The passerby] beside you waves to a friend across the street."
"To your left, [a passerby] drops her purse, and swears as she retrieves it."

Hair color is a kind of value. A person has hair color. the hair colors are red-headed, brunette, blonde.

Height is a kind of value. A person has height. The heights are tall, medium-height, short.

Grooming is a kind of value. A person has grooming. The groomings are messy and tidy.

To reset passerby:
    now the hair color of the passerby is a random hair color;
    now the height of the passerby is a random height;
    now the grooming of the passerby is a random grooming.

The passerby is a woman in Riverside Drive. The passerby is scenery. Understand "woman" or "lady" as the passerby. The printed name of the passerby is "[one of]woman[or]lady[purely at random]".

At this point we borrow some techniques from later to describe the woman with a random combination of characteristics, and to let the player refer to her by those traits:

Before printing the name of the passerby:
    if a random chance of 1 in 4 succeeds, say "[height] ";
    if a random chance of 1 in 4 succeeds, say "[grooming] ";
    if a random chance of 1 in 4 succeeds, say "[hair color] ".

Understand the hair color property as describing the passerby. Understand the height property as describing the passerby. Understand the grooming property as describing the passerby.

If we also wanted each of those combinations to mean some more specifically-described woman:

Instead of examining a passerby:
    repeat through Table of Passerby Descriptions:
        if hair entry is the hair color of the passerby and height entry is the height of the passerby and grooming entry is the grooming of the passerby, say "[description entry][paragraph break]".

Table of Passerby Descriptions

hair

height

grooming

description

red-headed

tall

messy

"An older woman with long red hippie-hair poking out of a ponytail in straggles, and bent to hide how tall she is."

red-headed

medium-height

messy

"A shaggy red-head with shingled hair."

red-headed

short

messy

"Almost an urchin, and very young, with ginger hair and a smudged nose and far too many freckles."

red-headed

tall

tidy

"A precise career woman with henna-red hair."

red-headed

medium-height

tidy

"Her hair is red in the way that lollipops and fire trucks are red: not by nature but by art. The rest of her clothing is pretty ordinary, though."

red-headed

short

tidy

"Thin and small in every sense, with chin-length red-hair. Even high heels do not bring her head much above your shoulder."

brunette

tall

messy

"A Juno-esque woman with dark hair, wearing something resembling a tent."

brunette

medium-height

messy

"An unremarkable woman with dark brown hair and the aura of needing a wash."

brunette

short

messy

"There are mustard stains on the t-shirt of this short brown-haired woman. Estimated age ca. 40. Possibly homeless."

brunette

tall

tidy

"A leggy brunette in business attire."

brunette

medium-height

tidy

"Medium-height, brown-haired, generally nondescript."

brunette

short

tidy

"A neat little dark-haired girl."

blonde

tall

messy

"A tall blonde of about thirteen who looks as though she has not yet figured out how to get her wardrobe to catch up with her rate of growth. Her t-shirt and her pants are too short."

blonde

medium-height

messy

"Black leather pants and the wall-o-hair look."

blonde

short

messy

"One of those shocking platinum blonde types, with a tiger-patterned skirt. Reeally trashy."

blonde

tall

tidy

"Elfin and severe, with perfectly straight hair falling to the middle of the back."

blonde

medium-height

tidy

"A rounded, Marilyn-esque blonde."

blonde

short

tidy

"Pin-precise in a blue-and-white striped suit and a boyish haircut."

Test me with "z / z / x passerby / z / z / x passerby".

Suppose we have an urban space we want to populate with random passers-by. These should have a range of characteristics and not always be described in the same way; and once the player has noticed one, he should be able to look at her further, until another pedestrian crosses his path.

paste.png "Uptown Girls"

Riverside Drive is a room. "There's a pleasant late-afternoon view of the Hudson, and a snap in the air, and you would rather be here than anywhere."

Instead of going a direction, say "Oh, you know where you're going; no need to deviate from the usual path."

Instead of waiting, say "You stroll along enjoying the November crispness."

It gets a little annoying to have a random event occurring every single turn of play, so let's introduce some randomness to determine how often the message appears:

Every turn when a random chance of 1 in 3 succeeds:
    reset passerby;
    choose a random row in the Table of Atmospheric Events;
    say "[event entry][paragraph break]"

Table of Atmospheric Events
event
"Slowly [a passerby] strolls by, turning to look at you as she passes."
"Some [passerby] nearly bumps into you."
"You dodge to avoid [a passerby]."
"You weave around [a passerby], who has stalled to look into a window."
"There's a ruckus as one of the ubiquitous taxis nearly collides with [a passerby] crossing the street."
"[The passerby] beside you waves to a friend across the street."
"To your left, [a passerby] drops her purse, and swears as she retrieves it."

Hair color is a kind of value. A person has hair color. the hair colors are red-headed, brunette, blonde.

Height is a kind of value. A person has height. The heights are tall, medium-height, short.

Grooming is a kind of value. A person has grooming. The groomings are messy and tidy.

To reset passerby:
    now the hair color of the passerby is a random hair color;
    now the height of the passerby is a random height;
    now the grooming of the passerby is a random grooming.

The passerby is a woman in Riverside Drive. The passerby is scenery. Understand "woman" or "lady" as the passerby. The printed name of the passerby is "[one of]woman[or]lady[purely at random]".

At this point we borrow some techniques from later to describe the woman with a random combination of characteristics, and to let the player refer to her by those traits:

Before printing the name of the passerby:
    if a random chance of 1 in 4 succeeds, say "[height] ";
    if a random chance of 1 in 4 succeeds, say "[grooming] ";
    if a random chance of 1 in 4 succeeds, say "[hair color] ".

Understand the hair color property as describing the passerby. Understand the height property as describing the passerby. Understand the grooming property as describing the passerby.

If we also wanted each of those combinations to mean some more specifically-described woman:

Instead of examining a passerby:
    repeat through Table of Passerby Descriptions:
        if hair entry is the hair color of the passerby and height entry is the height of the passerby and grooming entry is the grooming of the passerby, say "[description entry][paragraph break]".

Table of Passerby Descriptions

hair

height

grooming

description

red-headed

tall

messy

"An older woman with long red hippie-hair poking out of a ponytail in straggles, and bent to hide how tall she is."

red-headed

medium-height

messy

"A shaggy red-head with shingled hair."

red-headed

short

messy

"Almost an urchin, and very young, with ginger hair and a smudged nose and far too many freckles."

red-headed

tall

tidy

"A precise career woman with henna-red hair."

red-headed

medium-height

tidy

"Her hair is red in the way that lollipops and fire trucks are red: not by nature but by art. The rest of her clothing is pretty ordinary, though."

red-headed

short

tidy

"Thin and small in every sense, with chin-length red-hair. Even high heels do not bring her head much above your shoulder."

brunette

tall

messy

"A Juno-esque woman with dark hair, wearing something resembling a tent."

brunette

medium-height

messy

"An unremarkable woman with dark brown hair and the aura of needing a wash."

brunette

short

messy

"There are mustard stains on the t-shirt of this short brown-haired woman. Estimated age ca. 40. Possibly homeless."

brunette

tall

tidy

"A leggy brunette in business attire."

brunette

medium-height

tidy

"Medium-height, brown-haired, generally nondescript."

brunette

short

tidy

"A neat little dark-haired girl."

blonde

tall

messy

"A tall blonde of about thirteen who looks as though she has not yet figured out how to get her wardrobe to catch up with her rate of growth. Her t-shirt and her pants are too short."

blonde

medium-height

messy

"Black leather pants and the wall-o-hair look."

blonde

short

messy

"One of those shocking platinum blonde types, with a tiger-patterned skirt. Reeally trashy."

blonde

tall

tidy

"Elfin and severe, with perfectly straight hair falling to the middle of the back."

blonde

medium-height

tidy

"A rounded, Marilyn-esque blonde."

blonde

short

tidy

"Pin-precise in a blue-and-white striped suit and a boyish haircut."

Test me with "z / z / x passerby / z / z / x passerby".