Please enable JavaScript to use CodeHS

Randomization in JavaScript

By Ryan Hart

Randomization plays an important role in many applications like video games, physics modeling, cryptography, and even art and music! To start, JavaScript has a function Math.random() that generates a decimal number between 0 and 1 (inclusive of 0, but not 1). If you would like to generate an integer in a different range, you can apply the following calculation:

var randomInteger = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);

where maxInt and minInt are the inclusive limits of your range, and Math.floor rounds the calculation down to the nearest integer. See the example below for this in action.

  • Try changing the values of maxInt and minInt to see what happens. Can they be negative integers?
  • Try removing Math.floor from the calculation. What happens when you don’t round the calculation?

Randomization on CodeHS

While you are welcome to use Math.random() and the corresponding calculations to adjust your range, the CodeHS library has a handful of randomization functions that you can use to make your programming simpler:

Random Integer (inclusive): Randomizer.nextInt(min, max) (generates a random integer between min and max integers)
Random Float (inclusive): Randomizer.nextFloat(min, max) (generates a random float (ie decimal number) between min and max numbers)
Random Boolean: Randomizer.nextBoolean() (generates a random true or false boolean value)
Random Color: Randomizer.nextColor() (generates a random hexadecimal color code in the format #RRGGBB)

Take a look at all four of these randomizations in the example below.

Random examples

Below are a couple of examples to see different uses of the CodeHS Randomizer functions.

This first example is a snippet from an adventure video game. The randomizer is called when the player opens a treasure chest, generating a random number (and associated item) for the player to find.

  • Try expanding the range of the Randomizer and adding another possible item to the chest!


The next example randomizes the color of the face’s eyes every time the user clicks on the canvas. You can find the color randomizer in the function colorChange().

  • Using the current code in the function colorChange(), can you also have the color of the face and/or mouth change colors every time the user clicks?

Now it’s your turn!

As you can see, there are a wide variety of uses for randomization in programming. Using the space below, try implementing one of the following tasks, or come up with something of your own!

  • Create a random name generator so that each time the program is run, it prints out a random name (hint: use Treasure Chest example as guidance).
  • Create a dice rolling program that prints out a number between 1 and 6 each time the program is run.
  • Create a lie detection program that prints out true or false when the user runs a program and enters a fact (hint: use readLine("Enter fact: "); to get the user input).