Generate Random Number Between Two Numbers in JavaScript

Generate random number between two numbers in JavaScript

Important

The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.

If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:

    const rndInt = Math.floor(Math.random() * 6) + 1
console.log(rndInt)

Generating random whole numbers in JavaScript in a specific range

There are some examples on the Mozilla Developer Network page:

/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}

/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
|
[0 .................................... 1)
[0 .................................... max - min)
|
x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
└───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘ ← Math.round()
min min+1 max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval)
| | | | | |
└───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘ ← Math.floor()
min min+1 max-1 max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

How to Generate a random number of fixed length using JavaScript?

console.log(Math.floor(100000 + Math.random() * 900000));

Generate unique random numbers between 1 and 100

For example: To generate 8 unique random numbers and store them to an array, you can simply do this:

var arr = [];

while(arr.length < 8){

var r = Math.floor(Math.random() * 100) + 1;

if(arr.indexOf(r) === -1) arr.push(r);

}

console.log(arr);

Javascript Random Integer Between two Numbers

Your problem is you never converted your string to numbers try adding this

if (  numCount.match(/^[\d]*$/ ) && 
randNumMin.match(/^[\d]*$/ ) &&
randNumMax.match(/^[\d]*$/ )){
if (numCount === "" || randNumMin === "" || randNumMax === "") {
alert ("Please fill out all forms then try again.");
} else {
numCount=numCount-0;randNumMin=randNumMin-0;randNumMax=randNumMax-0;

Another note you need to change your checking if the value is an empty string to strict equality. To see what I mean try using zero for one of the values. 0 == ""//returns true because both are falsy 0 === ""//returns false.

Best way to generate random number between two sets of values

Generate a number between 10 and 40 and then generate a 1 or 0, if 0 convert it to negative 1 and multiply by either result
Math.random() * 30 + (Math.random() < 0.5 ? -40 : 10);



Related Topics



Leave a reply



Submit