Random
The random ()
function in p5 (which you can read about here) is similar to the more common Math.random ()
function, (which you can read about here), but with some extra features built in.
No arguments
Just by itself, p5's random ()
behaves exactly like Math.random ()
-- it returns a value between 0
and 1
. It might very, very occasionally return a value of exactly 0
, but it will never return a value of exactly 1
, only values very close to it.
One numerical argument
Giving p5's random ()
a single numerical value specifies a maximum. In this case, the function returns a random value between 0
and that maximum. For example, random (2)
will return a decimal value between 0
and 2
.
Two numerical arguments
Giving p5's random ()
two numerical values specifies a minimum, and a maximum. For example random (2, 8)
will return a random decimal value between 2
and 8
.
An array
If you give p5's random ()
function an array, it will select one of the elements of that array at random, and return it.