Skip to main content

roulette_wheel

Discworld driver help

roulette_wheel

Name

roulette_wheel() - pick an element with a weighted chance at random

Synopsis

mixed roulette_wheel( mapping stuff );

Description

The roulette_wheel() efun takes a mapping representing weighted chances, and returns an element from that mapping according to the weightings.

For example, if we have the mapping:

m = ([ "bing" : 10, "bong" : 5, "bang" : 1 ]);

Calling the roulette_wheel() function with this mapping will sum up all of the weights (16), and then return one of the three strings based on their weights, so on a long enough run it will return "bing" ten out of every sixteen times, "bong" five out of every sixteen times, and "bang" once out of every sixteen times.

The values (weights) of the mapping all need to be non-negative integers, or the function will runtime.

See also

element_of(), random(), shuffle()