randomNumber(min, max)
Category:Math
You will find many opportunities in your apps to utilize random numbers. Any numeric function parameter with a valid range of values can be randomized.
Examples
Example: generate a random number
// Generates a random number in the range 5 to 20 (inclusive).
console.log(randomNumber(5, 20));
Example: random circle
Draw an orange circle at a random place between the middle and left hand side of the screen
// Draws a randomly wide ellipse at a random location horizontally
background("tomato");
fill("orange");
ellipse(randomNumber(100,300), 200, randomNumber(50,80), 50);
Syntax
randomNumber(min, max);
Parameters
Name | Type | Required? | Description |
---|---|---|---|
min | number | The minimum number returned | |
max | number | The maximum number returned |
Returns
Returns a random number in the range min to max (inclusive). The number returned will always be an integer.
Tips
- Negative values for parameters min or max are allowed.
- If you accidentally make min larger than max it will still return a random number in the range.
- The number returned is not truly random as defined in mathematics but is pseudorandom.
Found a bug in the documentation? Let us know at support@code.org.