如何在JavaScript中的两个指定变量之间生成随机整数,例如x=4和y=8将输出4、5、6、7、8中的任意一个?
当前回答
加密功能强
要获取[x,y]范围内的加密强随机整数,请尝试:
让cs=(x,y)=>x+(y-x+1)*crypto.getRandomValues(新Uint32Array(1))[0]/2**32|0控制台日志(cs(4,8))
其他回答
要获得一个介于1和6之间的随机数,首先执行以下操作:
0.5 + (Math.random() * ((6 - 1) + 1))
这会将一个随机数乘以6,然后再加上0.5。接下来,通过以下操作将该数舍入为正整数:
Math.round(0.5 + (Math.random() * ((6 - 1) + 1))
这将数字四舍五入到最接近的整数。
或者为了更容易理解,请执行以下操作:
var value = 0.5 + (Math.random() * ((6 - 1) + 1))
var roll = Math.round(value);
return roll;
通常,使用变量执行此操作的代码为:
var value = (Min - 0.5) + (Math.random() * ((Max - Min) + 1))
var roll = Math.round(value);
return roll;
从最小值中减去0.5的原因是,仅使用最小值可以获得比最大值多1的整数。通过从最小值中减去0.5,基本上防止了最大值被舍入。
伊奥努·G·斯坦写了一个很棒的答案,但对我来说有点太复杂了。因此,我在Math.floor(Math.random()*(max-min+1))+min)中找到了对相同概念的更简单的解释。
注意:在阅读Jason的解释之前,你应该知道的唯一重要的事情是“truncate”的定义。他在描述Math.floor()时使用了这个术语。牛津词典将“truncate”定义为:
通过切掉顶端或末端来缩短(某物)。
下面是一个JavaScript函数的示例,它可以在不使用Math.random()的情况下生成任意指定长度的随机数:
function genRandom(length)
{
const t1 = new Date().getMilliseconds();
var min = "1", max = "9";
var result;
var numLength = length;
if (numLength != 0)
{
for (var i = 1; i < numLength; i++)
{
min = min.toString() + "0";
max = max.toString() + "9";
}
}
else
{
min = 0;
max = 0;
return;
}
for (var i = min; i <= max; i++)
{
// Empty Loop
}
const t2 = new Date().getMilliseconds();
console.log(t2);
result = ((max - min)*t1)/t2;
console.log(result);
return result;
}
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;
}
这是背后的逻辑。这是一个简单的三条规则:
Math.random()返回一个介于0(含)和1(不含)之间的数字。所以我们有一个这样的间隔:
[0 .................................... 1)
现在,我们需要一个介于min(含)和max(不含)之间的数字:
[0 .................................... 1)
[min .................................. max)
我们可以使用Math.random获取[min,max)区间的对应值。但是,首先我们应该通过从第二个区间减去min来稍微考虑一下问题:
[0 .................................... 1)
[min - min ............................ max - min)
这给出了:
[0 .................................... 1)
[0 .................................... max - min)
我们现在可以应用Math.random,然后计算对应的。让我们选择一个随机数:
Math.random()
|
[0 .................................... 1)
[0 .................................... max - min)
|
x (what we need)
因此,为了找到x,我们需要:
x = Math.random() * (max - min);
别忘了把min加回来,这样我们就可以得到[分钟,最大值]间隔内的数字:
x = Math.random() * (max - min) + min;
这是MDN的第一个函数。第二个,返回一个介于min和max之间的整数,两者都包含在内。
现在,对于整数,可以使用round、ceil或floor。
您可以使用Math.round(Math.random()*(max-min))+min,但这给出了一个非偶数分布。最小值和最大值都只有大约一半的掷骰机会:
min...min+0.5...min+1...min+1.5 ... max-0.5....max
└───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘ ← Math.round()
min min+1 max
如果将最大值排除在区间之外,那么它比最小值滚动的机会更小。
使用Math.floor(Math.random()*(max-min+1))+min,您可以获得完全均匀的分布。
min... min+1... ... max-1... max.... (max+1 is excluded from interval)
└───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘ ← Math.floor()
min min+1 max-1 max
你不能在这个等式中使用ceil()和-1,因为max现在的掷骰机会稍微少了一些,但你也可以掷(不需要的)min-1的结果。
使用此函数获取给定范围内的随机数:
function rnd(min, max) {
return Math.floor(Math.random()*(max - min + 1) + min);
}