There are some examples on the Mozilla Developer Network page:
(Mozilla开发人员网络页面上有一些示例:)
/**
* 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).
(Math.random()
返回一个介于0(含)和1( Math.random()
之间的Number
。)
So we have an interval like this: (所以我们有一个这样的间隔:)
[0 .................................... 1)
Now, we'd like a number between min
(inclusive) and max
(exclusive):
(现在,我们想要一个介于min
(含)和max
(不含)之间的数字:)
[0 .................................... 1)
[min .................................. max)
We can use the Math.random
to get the correspondent in the [min, max) interval.
(我们可以使用Math.random
在[min,max)间隔中获取对应的对象。)
But, first we should factor a little bit the problem by subtracting min
from the second interval: (但是,首先我们应该通过从第二个间隔中减去min
来解决这个问题:)
[0 .................................... 1)
[min - min ............................ max - min)
This gives:
(这给出:)
[0 .................................... 1)
[0 .................................... max - min)
We may now apply Math.random
and then calculate the correspondent.
(现在我们可以应用Math.random
,然后计算对应的对象。)
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
,我们要做:)
x = Math.random() * (max - min);
Don't forget to add min
back, so that we get a number in the [min, max) interval:
(别忘了加min
,这样我们就可以在[min,max)间隔中得到一个数字:)
x = Math.random() * (max - min) + min;
That was the first function from MDN.
(这是MDN的第一个功能。)
The second one, returns an integer between min
and max
, both inclusive. (第二个,返回介于min
和max
之间(包括两个端点)的整数。)
Now for getting integers, you could use round
, ceil
or floor
.
(现在要获取整数,可以使用round
, ceil
或floor
。)
You could use Math.round(Math.random() * (max - min)) + min
, this however gives a non-even distribution.
(您可以使用Math.round(Math.random() * (max - min)) + min
,但这会产生不均匀的分布。)
Both, min
and max
only have approximately half the chance to roll: (min
和max
滚动率只有大约一半:)
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
.
(如果从间隔中排除了max
,则滚动的机会比min
还要少。)
With Math.floor(Math.random() * (max - min +1)) + min
you have a perfectly even distribution.
(使用Math.floor(Math.random() * (max - min +1)) + min
您可以获得完美的均匀分布。)
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.
(您不能在该方程式中使用ceil()
和-1
,因为max
现在滚动的机会略少,但是您也可以滚动(不需要的) min-1
结果。)