Skip to main content

surprise

A surprise block is used to randomly choose between multiple options. Only one of the options will be executed.

The simplest form of a surprise block is a list of options. All options are given equal chance of being selected.

surprise {
ShieldPowerup,
TriblastPowerup,
}

Weighted Random

It is possible to assign weights to each option to control the likelihood of each option being selected.

surprise {
2 => TriblastPowerup,
1 => ShieldPowerup,
}

In this example, the TriblastPowerup is twice as likely to be selected as the ShieldPowerup.

Multiple Statements

Each option can be a block of code instead of a single statement:

surprise {
{
Print { "You got a Triblast Powerup!" }
TriblastPowerup
},
{
Print { "You got a Shield Powerup!" }
ShieldPowerup
},
}

Returning Values

A surprise block can also be used as an expression, in which case the resulting value will be the value of the selected option. The last expression of the block determines the resulting value of the option.

let x = surprise {
"apple",
"banana",
}
// x is either "apple" or "banana", chosen randomly

To return multiple values from a surprise block, separate them with commas and wrap them in parentheses:

let name, points = surprise {
("apple", 100),
("banana", 200),
}
// Either name="apple" and points=100, or name="banana" and points=200, chosen randomly

You can also have multiple statements before the final expression when returning multiple values:

let name, points = surprise {
{
Print { "You got an apple!" }
("apple", 100)
},
{
Print { "You got a banana!" }
("banana", 200)
},
}
// Either name="apple" and points=100, or name="banana" and points=200, chosen randomly

Specifying the Random Number Stream

You can also provide an ID to the surprise block to choose its random number stream. See Random Number Generator Streams

surprise<apple> {
Smitten,
Poisoned,
}