How can I generate a random whole number between two specified variables in Javascript, e.g. x=4 and y=8 would output any of 4,5,6,7,8?
There are some examples on the Mozilla Developer Center page:
/**
* Returns a random number between min and max
*/
function getRandomArbitary (min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt (min, 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 MDC. The second one, returns an integer between min
and max
, both inclusive. In order to do that, a little trick is applied. The second interval is increased to max
+ 1 exclusive, so that max
is a value inside the interval, thus a candidate for the final result:
Math.random()
|
[0 .................................... 1)
[min ................................... max)
// increase interval
[min ..............................max.. max + 1)
// factor out min
[0 ................................max.. max - min + 1)
|
X (what we need)
Now, the above equation becomes:
x = Math.random() * (max - min + 1) + min;
Which may yield max
as the value of x
. But we want an integer, so we use Math.floor
:
x = Math.floor(Math.random() * (max - min + 1)) + min;
function getRandomizer(a,b) {
return function() {
return (Math.floor( Math.random()* (b-a) ) ) + a;
}
}
usage:
var rollDie = getRandomizer(1,6);
while (true) {
alert( rollDie() );
}
breakdown:
We are returning a function (borrowing from functional programming) that when called, will return a random number between the the values a and b used to construct it.
(a is lower number, b is greater number)
Math.random() * (b-a)
Math.random returns a random double between 0 and 1. So multiplying it by the smaller number will give us a range we want (the difference between a and b).
Math.floor( Math.random() * (b-a) )
Math.floor rounds the number down to the nearest int. So we now have all the ints between 0 and b-a.
Math.floor( Math.random()* (b-a) ) + a
a is our lower value the difference between the two numbers, so after adding that, we will get numbers between a and b (inclusive). :D
If you pass in a non-int value or greater number first, you'll get undesirable behavior.
;D
Doing a Google search for "javascript random number" yields the following web page which explains how to do it:
function getRandom(int lower, int upper)
{
return lower + (Math.random() * (upper - lower));
}
var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;