Algosim documentation: random

random

Represents a random element in a container.

Syntax

The returned value is an lvalue unless X is a set.

Description

If X is any container, then random(X) is a randomly chosen element of it.

X may be a vector, a matrix, a list, a set, a structure, a string, or a pixmap.

Unless S is a set, random(X) is an lvalue, so it may be assigned to. The right hand side must be an object of suitable type. For instance, if X is a real vector, the value must be a real number; if X is a string, the value must be a character.

Examples

random({ "cookies", "banana", "carrot" })
cookies
random(SequenceVector(1000) @ prime)
1879
random(functions())
MillisecondOfTheWeek
random(NamedColors())
name: darkslateblue
color: rgba(0.282, 0.239, 0.545, 1.000)
A ≔ ❨❨4, 1, 2❩, ❨6, 7, 2❩❩
⎛4  1  2⎞
⎝6  7  2⎠
random(A)
6
random(A) ≔ 10
10
A
⎛ 4   1   2⎞
⎝ 6  10   2⎠
bm ≔ ComputePixmap(200, 200, x, y, rgb(x/199, y/199, 0.5));
for(i, 1, 5000, (random(bm) ≔ color("gold")));
scale(bm, 2)

Image 1

See also