Reference random()

random()

Returns a random number or a random element from an array.

random() follows uniform distribution, which means that all outcomes are equally likely. When random() is used to generate numbers, all numbers in the output range are equally likely to be returned. When random() is used to select elements from an array, all elements are equally likely to be chosen.

By default, random() produces different results each time a sketch runs. The randomSeed() function can be used to generate the same sequence of numbers or choices each time a sketch runs.

The version of random() with no parameters returns a random number from 0 up to but not including 1.

The version of random() with one parameter works one of two ways. If the argument passed is a number, random() returns a random number from 0 up to but not including the number. For example, calling random(5) returns values between 0 and 5. If the argument passed is an array, random() returns a random element from that array. For example, calling random(['🦁', '🐯', '🐻']) returns either a lion, tiger, or bear emoji.

The version of random() with two parameters returns a random number from a given range. The arguments passed set the range's lower and upper bounds. For example, calling random(-5, 10.2) returns values from -5 up to but not including 10.2.

Examples

Syntax

random([min], [max])
random(choices)

Parameters

min
Number:

lower bound (inclusive).

max
Number:

upper bound (exclusive).

choices
Array:

array to choose from.

Returns

Number: random number.
Notice any errors or typos? Please let us know. Please feel free to edit src/math/random.js and open a pull request!

Related References