To create a random number in Javascript, the math.random()
function is used. JavaScript math.random()
generates a random decimal value between 0 and 1. The function does not take any arguments. To get random numbers higher than 1 simply multiply the result according to the need.
In programming, there are often instances where you need to retrieve a random number. For example, you may be creating a guessing game and want your program to generate a random number every time the user runs your program. Or you may want to run a function a random number of times.
That’s where the JavaScript Math.random() function comes in. Math.random() is a built-in method that can be used to generate random numbers in JavaScript. The function returns a value between 0 (inclusive) and 1 (exclusive), but we can use another function called Math.floor() to turn our number into a whole random number.
In this tutorial, we are going to explore the basics of JavaScript random number generation, and discuss how you can use the Math.random() function in your code.
JavaScript Math Refresher
The JavaScript Math object offers developers a number of mathematical functions which can be performed on numbers. These functions include returning the absolute value of a number, rounding a number, calculating the square root of a number, among others.
The Math object works with the Number data type in JavaScript. Here’s what the Math object looks like in a JavaScript program:
Math.functionName();
One of the most useful functions in the Math library — and one that we will use later in this tutorial — is the Math.floor() function. This method can be used to deliver a value rounded downwards to the closest integer, or whole number. Here’s an example of the Math.floor() method in action:
console.log(Math.floor(5.7));
Our code returns: 5. The number the Math.floor() function processed was rounded down, which is why our program returned “5” instead of “6.”
Similarly, the Math.round() function can be used to round a number up to the nearest value. Here’s an example of the Math.round() function in action:
console.log(Math.round(5.7));
Our code returns: 6.
The Math object includes a number of other mathematical functions that can be used to work with numbers, but the main ones you need to know about to generate a JavaScript random number are Math.floor() and Math.random().
JavaScript Math.random()
The JavaScript Math.random() method returns a random number between 0 (inclusive) and 1 (exclusive). The Math.random() function returns a floating point pseudo-random number, which means it will include a decimal. The syntax for the Math.random() method is as follows:
Math.random();
Here’s an example of console.log() being used to print out a random number:
console.log(Math.random());
Our program returns:
0.022831222528214346
As you can see, our program has returned a random number between zero and one. But this number is rather small, and many applications that need random numbers want to generate larger numbers. That’s where the Math.floor() function comes in.
We can use the Math.floor() function to round our random number up to become a random integer, and we can multiply that number to generate a larger one. Here’s an example of a random number generator function in JavaScript:
var random_number = Math.floor(Math.random() * 10); console.log(random_number);
Here’s the result of our program after running it three times:
5 5 1
As you can see, our program has generated a random number. In the above example, we used “* 10”
to generate a larger number. That function helps us retrieve a number between “1”
and “10.”
If we wanted a larger number, we could change our multiple like so:
var random_number = Math.floor(Math.random() * 100); console.log(random_number);
After running our program three times, the following numbers were generated:
44 7 26
So, let’s say that we are creating an addition game and we want to generate two random numbers between “1” and “100” for our addition sum to include. We could use the following code to generate our random numbers:
var number_one = Math.floor(Math.random() * 100); var number_two = Math.floor(Math.random() * 100); console.log(`What is ${number_one} + ${number_two}?`);
Our program returns:
What is 93 + 56?
In the above example, we first generated two random numbers using the Math.random() function. Then, we printed out a statement that asks the user “What is [our first number] + [our second number]?”
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
JavaScript Random Number Between Two Values
Math.random() does not include any arguments, which means we cannot use the function to generate a number between two values. However, we can create a custom method that allows us to generate a random number between two values.
Let’s say that we are creating an addition game that should generate two numbers between 100 and 200 for a more advanced level.
Here’s an example function that we could use to generate those numbers:
const generateRandomNumber = (min, max) => { return Math.floor(Math.random() * (max - min) + min); }; console.log(generateRandomNumber(100, 200));
When we run our program three times, the following values are returned:
152 186 158
As you can see, our program generates random numbers within the range of 100 and 200. Let’s break down our program and explore how it works in more depth.
On the first line, we define a function called “generateRandomNumber().”
Our function takes in two arguments: the minimum value and the maximum value of the range of numbers within which our random number should be generated. On the next line, we use the Math.random() function to generate a random number, and our “max”
and “min”
variables to ensure the number we generate will be in our range.
Then, on the final line, we call our generateRandomNumber() function and pass it the values 100 and 200. So, when we run our program, a random number between 100 and 200 is generated.
Conclusion
The Math.random() JavaScript function can be used to generate random numbers between 0 (inclusive) and 1 (exclusive). Then, by using the Math.floor() function, we can generate larger numbers based on our needs.
In this tutorial, we explored the basics of the JavaScript Math library and discussed how to use the Math.random() method to generate a random number. We also created a custom function that can be used to generate a random number between two values.
Now you have the knowledge you need to generate your own random numbers in JavaScript like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.