Академический Документы
Профессиональный Документы
Культура Документы
Reference
Search reference
random()
Examples
https://p5js.org/reference/#/p5/random 1/3
5/6/2020 reference | random()
edit
// Get a random element from an array using thereset copy
random(Arra
let words = ['apple', 'bear', 'cat', 'dog'];
let word = random(words); // select random word
text(word, 10, 50); // draw the word
Description
Return a random floating-point number.
Takes either 0, 1 or 2 arguments.
If no argument is given, returns a random number from 0 up to (but not
including) 1.
If one argument is given and it is a number, returns a random number from
0 up to (but not including) the number.
If one argument is given and it is an array, returns a random element from
that array.
If two arguments are given, returns a random number from the first
argument up to (but not including) the second argument.
Syntax
random([min], [max])
random(choices)
Parameters
min
Number: the lower bound (inclusive) (Optional)
https://p5js.org/reference/#/p5/random 2/3
5/6/2020 reference | random()
max
Number: the upper bound (exclusive) (Optional)
choices
Array: the array to choose from
Returns
Number: the random number
Notice any errors or typos? Please let us know. Please feel free to edit src/math/random.js
and issue a pull request!
p5.js was created by Lauren McCarthy and is developed by a community of collaborators, with support from the Processing Foundation and NYU ITP.
Identity and graphic design by Jerel Johnson. © Info.
https://p5js.org/reference/#/p5/random 3/3