JavaScript

# How to generate a random number in JavaScript

In this tutorial, we are going to see how to generate a random number using the Math.random() function. The Math.random() method returns a random number between 0(inclusive) and 1(excluded), represented by [0, 1) in mathematics. This means it will return a random number such as 0.832000458799852 between the ranges 0 and 1, but never equal to 1.

###### Example 1: How to generate a random number in JavaScript
```var random = Math.random();

Output:

`0.6050978412398191`

###### Example 2: How to generate a random number in JavaScript

The Math.random() method can be used to get a random number between two values. The returned value is greater than min and can optionally be equal to min (n >= min). It is also less than and not equal to max (n < max).

```var min=1;
var max=10;
var random = Math.random() * (max - min) + min;

Output:

`9.884460776017944`

###### Example 3: How to generate a random number in JavaScript

We can use this randomly generated number with the Math.floor() method to generate a random number without a comma.

```var min=1;
var max=10;
var random = Math.floor(Math.random() * (max - min)) + min;
`4` MCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More