randomNumber(min, 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.
Examples
Example: Generate random number in range
// Generates a random number in the range 5 to 20 (inclusive).
console.log(randomNumber(5, 20));
Example: Random Walk
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();
Example: Clouds
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));
}

Syntax
randomNumber(min, max);
Parameters
Name | Type | Required? | Description |
---|---|---|---|
min | number | The minimum number returned | |
max | number | The maximum number returned |
Returns
Tips
- Negative values for parameters min or max are allowed.
- If you accidently 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.