# 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

## 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();

Try it on StackBlitz

### How to get a random number between 0 (inclusive) and 1 (inclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Add 1 to the maximum value (1).
3. Multiply step one's output with step two's result to get a random number between 0 (inclusive) and 1 (inclusive).
Math.random() * (1 + 1);

Try it on StackBlitz

### How to get a random number between 0 (inclusive) and 3 (exclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Multiply Math.random()'s output by 3 to get a random number between 0 (inclusive) and 3 (exclusive).
Math.random() * 3;

Try it on StackBlitz

### How to get a random number between 0 (inclusive) and 3 (inclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Add 1 to the maximum value (3).
3. Multiply step one's output with step two's result to get a random number between 0 (inclusive) and 3 (inclusive).
Math.random() * (3 + 1);

Try it on StackBlitz

### How to get a random integer between 0 (inclusive) and 3 (exclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Multiply Math.random()'s output by 3 to get a random number between 0 (inclusive) and 3 (exclusive).
3. 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);

Try it on StackBlitz

### How to get a random integer between 0 (inclusive) and 3 (inclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Add 1 to the maximum value (3).
3. Multiply step one's output with step two's result to get a random number between 0 (inclusive) and 3 (inclusive).
4. 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);

Try it on StackBlitz

### How to get a random number between 5 (inclusive) and 10 (exclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Multiply 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).
3. Add the minimum value (5) to step two's result to get a random number between 5 (inclusive) and 10 (exclusive).
Math.random() * (10 - 5) + 5;

Try it on StackBlitz

### How to get a random number between 5 (inclusive) and 10 (inclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Subtract the minimum value (5) from the maximum (10).
3. Add 1 to step two's result.
4. Multiply step one's output with step three's result to get a random number between 0 (inclusive) and 5 (inclusive).
5. Add the minimum value (5) to step four's result to get a random number between 5 (inclusive) and 10 (inclusive).
Math.random() * (10 - 5 + 1) + 5;

Try it on StackBlitz

### How to get a random integer between 5 (inclusive) and 10 (inclusive)​

1. Invoke Math.random() to get a random number between 0 (inclusive) and 1 (exclusive).
2. Subtract the minimum value (5) from the maximum (10).
3. Add 1 to step two's result.
4. Multiply step one's output with step three's result to get a random number between 0 (inclusive) and 5 (inclusive).
5. Add the minimum value (5) to step four's result to get a random number between 5 (inclusive) and 10 (inclusive).
6. 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);

Try it on StackBlitz

## Overview​

This article discussed what JavaScript's Math.random() method does. We also used examples to see how it works.