randomImax

A function that can be used in Formulas.

Syntax and semantics

randomImax (distribution#)
sample from an array of relative probabilities.

Example

The following draws the outcome 1 one half of the time, the outcome 2 one sixth of the time, and the outcome 3 one third of the time:

for i to 20
outcome = randomImax ({ 3.0, 1.0, 2.0 })
appendInfoLine: outcome
endfor
=>
1
1
1
2
1
1
2
3
3
3
2
1
1
1
1
1
1
1
1
1

All elements of the vector have to be non-negative:

asserterror Element 2 is less than zero; cannot interpret as a probability.
a = randomImax ({ 3.0, -1.0, 2.0 })

Some elements of the vector may be zero. The following draws the outcome 1 60 percent of the time, and the outcome 3 40 percent of the time:

for i to 20
outcome = randomImax ({ 3.0, 0.0, 2.0 })
appendInfoLine: outcome
endfor
=>
1
1
1
3
1
1
1
1
3
1
3
1
3
3
1
1
1
1
3
3

Not all elements of the vector can be zero:

asserterror Cannot interpret a zero vector as probabilities.
a = randomImax ({ 0.0, 0.0, 0.0 })

Links to this page


© Paul Boersma 2026