Math.random() Method in JS – How to Generate Random Numbers
Whenever you invoke Math.random(), the method returns a random number between zero (inclusive) and one (exclusive).
In other words, Math.random()
outputs a random number that is equal to or greater than zero (0
) but less than one (1
).
note
Math.random()
is one of the built-in methods of the globalMath
object.- The global
Math
object is one of the standard built-in JavaScript objects.
Syntax of the Math.random()
Method
Math.random()
accepts no argument. Here is the syntax:
Math.random();
Examples
Below are examples of the Math.random()
method.
How to get a random number between 0
(inclusive) and 1
(exclusive)
Invoke Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).
Math.random();
How to get a random number between 0
(inclusive) and 1
(inclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Add
1
to the maximum value (1
). - Multiply step one's output with step two's result to get a random number between
0
(inclusive) and1
(inclusive).
Math.random() * (1 + 1);
How to get a random number between 0
(inclusive) and 3
(exclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Multiply
Math.random()
's output by3
to get a random number between0
(inclusive) and3
(exclusive).
Math.random() * 3;
How to get a random number between 0
(inclusive) and 3
(inclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Add
1
to the maximum value (3
). - Multiply step one's output with step two's result to get a random number between
0
(inclusive) and3
(inclusive).
Math.random() * (3 + 1);
How to get a random integer between 0
(inclusive) and 3
(exclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Multiply
Math.random()
's output by3
to get a random number between0
(inclusive) and3
(exclusive). - Use the
Math.floor()
method to round down step two's result to the nearest integer.
const randomNumberBetweenZeroAndOne = Math.random();
const randomNumberBetweenZeroAndThree = randomNumberBetweenZeroAndOne * 3;
const randomIntegerBetweenZeroAndThree = Math.floor(
randomNumberBetweenZeroAndThree
);
// Log randomIntegerBetweenZeroAndThree to the console:
console.log(randomIntegerBetweenZeroAndThree);
How to get a random integer between 0
(inclusive) and 3
(inclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Add
1
to the maximum value (3
). - Multiply step one's output with step two's result to get a random number between
0
(inclusive) and3
(inclusive). - Use the
Math.floor()
method to round down step three's result to the nearest integer.
const randomNumberBetweenZeroAndOne = Math.random();
const maximumValuePlusOne = 3 + 1;
const randomNumberRangingFromZeroToThree =
randomNumberBetweenZeroAndOne * maximumValuePlusOne;
const randomIntegerRangingFromZeroToThree = Math.floor(
randomNumberRangingFromZeroToThree
);
// Log randomIntegerRangingFromZeroToThree to the console:
console.log(randomIntegerRangingFromZeroToThree);
How to get a random number between 5
(inclusive) and 10
(exclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Multiply
Math.random()
's output with the difference between the maximum (10
) and the minimum (5
) value to get a random number between0
(inclusive) and5
(exclusive). - Add the minimum value (
5
) to step two's result to get a random number between5
(inclusive) and10
(exclusive).
Math.random() * (10 - 5) + 5;
How to get a random number between 5
(inclusive) and 10
(inclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Subtract the minimum value (
5
) from the maximum (10
). - Add
1
to step two's result. - Multiply step one's output with step three's result to get a random number between
0
(inclusive) and5
(inclusive). - Add the minimum value (
5
) to step four's result to get a random number between5
(inclusive) and10
(inclusive).
Math.random() * (10 - 5 + 1) + 5;
How to get a random integer between 5
(inclusive) and 10
(inclusive)
- Invoke
Math.random()
to get a random number between0
(inclusive) and1
(exclusive). - Subtract the minimum value (
5
) from the maximum (10
). - Add
1
to step two's result. - Multiply step one's output with step three's result to get a random number between
0
(inclusive) and5
(inclusive). - Add the minimum value (
5
) to step four's result to get a random number between5
(inclusive) and10
(inclusive). - Use the
Math.floor()
method to round down step five's result to the nearest integer.
const randomNumberBetweenZeroAndOne = Math.random();
const maximumMinusMinimumValue = 10 - 5;
const maximumMinusMinimumValuePlusOne = maximumMinusMinimumValue + 1;
const randomNumberRangingFromZeroToFive =
randomNumberBetweenZeroAndOne * maximumMinusMinimumValuePlusOne;
const randomNumberRangingFromFiveToTen = randomNumberRangingFromZeroToFive + 5;
const randomIntegerRangingFromFiveToTen = Math.floor(
randomNumberRangingFromFiveToTen
);
// Log randomIntegerRangingFromFiveToTen to the console:
console.log(randomIntegerRangingFromFiveToTen);
Overview
This article discussed what JavaScript's Math.random()
method does. We also used examples to see how it works.