# How to generate random number in Javascript

Sometimes while testing we often need to generate random numbers. Different language has its own functions to generate random numbers.

In web developing, we can do this from Javascript irrelavant to backend languages. In this article, we will also generate random numbers between specific range.

function returns random float numbers between 0 and 1.**Math.random()**

```
Math.random();
// 0.8213480830154087
```

We can get float numbers between specific range number by using it.

```
/**
* returns a random float between min (inclusive) and max (exclusive)
*/
function getRandomNumbersBetweenRange(min, max) {
return Math.random() * (max - min) + min;
}
```

function rounds off float number to integer. So if you want integer values, here you can get by the below function.**Math.floor()**

```
/**
* returns a random integer between min (inclusive) and max (exclusive)
*/
function getRandomNumbersBetweenRange(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
```

However the above functions doesn't include the maximun number. If you also want to include the maximum number, then there is

function.**Math.ceil()**

```
/**
* returns a random integer between min (inclusive) and max (inclusive)
*/
function getRandomNumbersBetweenRange(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
```

These are basics way to generate numbers, you can also create your own functions and use. Happy coding.

Copyright 2024 ErrorGram