randomNumber max

Category:Math

randomNumber(max)

Category: Math

Returns a pseudorandom number ranging from zero to max, including both zero and max in the range.

The number returned is not truly random as defined in mathematics.

Examples


Example 1

Turtle Example

dot(randomNumber(100));

Console Example

console.log(randomNumber(3));     // generates a pseudorandom number in the range of 0 to 3 and then prints
                                  //    it to the console

Example 2

Turtle Example -- move forward 25 times, turning right a random number of degrees between 0 and 45.

for (var i = 0; i < 25; i++) {
  moveForward();
  turnRight(randomNumber(45));
}

This example prints out 10 pseudorandom numbers (in the range 0 to 10) to the console.

for (var i = 0; i < 10; i++) {        // repeats the code inside of this block 10 times
  console.log(randomNumber(10));      // calculates a pseudorandom number in the range 0 to 10
                                      //    and then prints it to the console
}

Example 3

This example calculates 500 pseudorandom numbers in the range 0 to 5 and then creates a bar graph illustrating the occurrences of each number.

textLabel("zero", "zero: ", "forId");       // creates a "zero" text label
textLabel("one", "one: ", "forId");         // creates a "one" text label
textLabel("two", "two: ", "forId");         // creates a "two" text label
textLabel("three", "three: ", "forId");     // creates a "three" text label
textLabel("four", "four: ", "forId");       // creates a "four" text label
textLabel("five", "five: ", "forId");       // creates a "five" text label

var values = [0, 0, 0, 0, 0, 0];            // creates an array to hold the occurrences of each
                                            //    pseudorandom value

for (var i = 0; i < 500; i++) {             // repeats the code in this block 500 times
  values[randomNumber(5)]++;                // calculates a pseudorandom number in the range 1-5, and then
                                            //    increments the corresponding value in the array
}

hide();                                     // hides the turtle so it is no longer visible
penWidth(5);                                // sets the pen width to 5 pixels
turnRight(90);                              // turns the turtle 90 pixels to the right
for (var i = 0; i < 6; i++) {               // repeats the code in this block 6 times
  penUp();                                  // stops the turtle from leaving a trail behind it as it moves
  moveTo(50, 10 + i * 25);                  // moves the turtle to the correct position
  penDown();                                // starts leaving a trail behind the turtle as it moves
  moveForward(values[i]);                   // moves the turtle forward the corresponding value in the array
}

Syntax

randomNumber(max);

Parameters

Name Type Required? Description
max number Yes The maximum number returned

Returns

Returns a pseudorandom number in the range of 0 to max (inclusive). For example, if a 3 is passed to the function, it could return a 0, 1, 2, or 3. The number returned will always be an integer.

Tips

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