# 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`).

## Syntax of the `Math.random()` Method

`Math.random()` accepts no argument. Here is the syntax:

## 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).

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).

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).

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).

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.

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.

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).

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).

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.

Try it on StackBlitz