Вы находитесь на странице: 1из 3

5/6/2020 reference | random()

English Español 简体中文

Home, Editor, Download, Donate, Get Started,


Reference, Libraries, Learn, Examples, Books,
Community, Showcase, Forum, GitHub, Twitter

Reference
Search reference
random()

Examples

for (let i = 0; i < 100; i++) { edit reset copy


let r = random(50);
stroke(r * 5);
line(50, i, 50 + r, i);
}

for (let i = 0; i < 100; i++) { edit reset copy


let r = random(-50, 50);
line(50, i, 50 + r, i);
}

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

Вам также может понравиться