choose

choose <iterable>

Pick one element of an iterable at random.

Every element has an equal chance of being chosen.

Arguments

Returns

A uniformly-random element. Over an empty iterable it yields null.

Examples

after command (say) {
  do "say I think I will go [choose [words "north east south west"]]."
}

[choose ...] evaluates to one of the four words at random, so the mob says something like "I think I will go east."

after command (say) {
  let target [choose [creatures $self]]
  unless [isnull $target]
  do "wave $target"
}

target is bound to a random living creature in the owner's room; the unless guard skips the wave when the room is empty and [choose ...] returned null.

choose is one of the language's sources of non-determinism (see Iterables and iterables); two runs may pick different elements.

See also