Category: Math
Returns a random number in the closed range from min to max.
You will find many opportunities in your apps to utilize random numbers. For turtle drawing you can randomize all the movement functions, the pen RGB color, pen thickness, and dot size. Any numeric function parameter with a valid range of values can be randomized.
// Generates a random number in the range 5 to 20 (inclusive). console.log(randomNumber(5, 20));
Random Walk Do a "random walk" of 4 steps, turning a random number of degrees after each step.
// Do a "random walk" of 4 steps, turning a random number of degrees after each step. moveForward(); turnRight(randomNumber(-90, 90)); moveForward(); turnRight(randomNumber(-90, 90)); moveForward(); turnRight(randomNumber(-90, 90)); moveForward();
Clouds Draw a cloud mass using randomly sized dots at random locations near each other.
// Draw a cloud mass using randomly sized dots at random locations near each other. penColor("skyblue"); dot(300); penUp(); penRGB(245, 245, 245,0.3); moveTo(randomNumber(0, 320),randomNumber(0, 450)); for (var i = 0; i < 50; i++) { moveTo(getX()+randomNumber(-25, 25),getY()+randomNumber(-25, 25)); dot(randomNumber(25,50)); } |
randomNumber(min, max);
Name | Type | Required? | Description |
---|---|---|---|
min | number | Yes | The minimum number returned |
max | number | Yes | The maximum number returned |
Returns a random number in the range min to max (inclusive). The number returned will always be an integer.
Found a bug in the documentation? Let us know at documentation@code.org
Found a bug in the documentation? Let us know at documentation@code.org