Learn Flexbox with Images
Use beautiful images to learn CSS Flexbox.
Find out moreWhenever 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
).
Math.random()
MethodMath.random()
accepts no argument. Here is the syntax:
Math.random();
Below are examples of the Math.random()
method.
0
(inclusive) and 1
(exclusive)Invoke Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).
Math.random();
0
(inclusive) and 1
(inclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).1
to the maximum value (1
).0
(inclusive) and 1
(inclusive).Math.random() * (1 + 1);
0
(inclusive) and 3
(exclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).Math.random()
’s output by 3
to get a random number between 0
(inclusive) and 3
(exclusive).Math.random() * 3;
0
(inclusive) and 3
(inclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).1
to the maximum value (3
).0
(inclusive) and 3
(inclusive).Math.random() * (3 + 1);
0
(inclusive) and 3
(exclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).Math.random()
’s output by 3
to get a random number between 0
(inclusive) and 3
(exclusive).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);
0
(inclusive) and 3
(inclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).1
to the maximum value (3
).0
(inclusive) and 3
(inclusive).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);
5
(inclusive) and 10
(exclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).Math.random()
’s output with the difference between the maximum (10
) and the minimum (5
) value to get a random number between 0
(inclusive) and 5
(exclusive).5
) to step two’s result to get a random number between 5
(inclusive) and 10
(exclusive).Math.random() * (10 - 5) + 5;
5
(inclusive) and 10
(inclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).5
) from the maximum (10
).1
to step two’s result.0
(inclusive) and 5
(inclusive).5
) to step four’s result to get a random number between 5
(inclusive) and 10
(inclusive).Math.random() * (10 - 5 + 1) + 5;
5
(inclusive) and 10
(inclusive)Math.random()
to get a random number between 0
(inclusive) and 1
(exclusive).5
) from the maximum (10
).1
to step two’s result.0
(inclusive) and 5
(inclusive).5
) to step four’s result to get a random number between 5
(inclusive) and 10
(inclusive).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);