Search⌘ K

Random Number Generation and Return Values

Explore how to write JavaScript functions that generate random integers within specified bounds and understand how to use return values effectively. Learn to assign function results to variables and apply conditional logic to produce dynamic outcomes.

Random integers

We met the Math.random() method previously, which creates a random decimal between 000000 and 111111. We often need a random integer value in programs. In fact, we used the following code to choose a random number between 111111 and 101010101010 in the higher or lower game earlier:

Math.ceil(Math.random() * 10);

It’s a useful exercise to abstract this code into ...