有没有办法用JavaScript生成指定范围内的随机数?
例如:指定范围从1到6,随机数可以是1、2、3、4、5或6。
有没有办法用JavaScript生成指定范围内的随机数?
例如:指定范围从1到6,随机数可以是1、2、3、4、5或6。
当前回答
重要的
The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.如果你想得到一个介于1(且只有1)和6之间的随机整数,你可以计算:
const rndInt=数学地板(Math.random()*6)+1控制台日志(rndInt)
哪里:
1是起始编号6是可能结果的数量(1+start(6)-end(1))
其他回答
函数randomIntFromInterval(min,max){//包括min和maxreturn Math.floor(Math.random()*(max-min+1)+min)}常量rndInt=随机IntFromInterval(1,6)控制台日志(rndInt)
它的“额外”之处在于它允许不以1开头的随机间隔。例如,你可以得到一个从10到15的随机数。灵活性
这个简单的功能很方便,在任何情况下都可以使用(经过充分测试)。此外,结果的分布已经过充分测试,100%正确。
function randomInteger(pMin = 1, pMax = 1_000_000_000)
//Author: Axel Gauffre.
//Here: https://stackoverflow.com/a/74636954/5171000
//Inspired by: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values
//
//This function RETURNS A RANDOM INTEGER between pMin (INCLUDED) and pMax (INCLUDED).
// - pMin and pMax should be integers.
// - HOWEVER, if pMin and/or pMax are FLOATS, they will be ROUNDED to the NEAREST integer.
// - NEGATIVE values ARE supported.
// - The ORDER of the 2 arguments has NO consequence: If pMin > pMax, then pMin and pMax will simply be SWAPPED.
// - If pMin is omitted, it will DEFAULT TO 1.
// - If pMax is omitted, it will DEFAULT TO 1 BILLION.
//
//This function works in ANY cases (fully tested).
//Also, the distribution of the results has been fully tested and is 100% correct.
{
pMin = Math.round(pMin);
pMax = Math.round(pMax);
if (pMax < pMin) { let t = pMin; pMin = pMax; pMax = t;}
return Math.floor(Math.random() * (pMax+1 - pMin) + pMin);
}
如果您想覆盖负数和正数,并确保其安全,请使用以下方法:
JS解决方案:
function generateRangom(low, up) {
const u = Math.max(low, up);
const l = Math.min(low, up);
const diff = u - l;
const r = Math.floor(Math.random() * (diff + 1)); //'+1' because Math.random() returns 0..0.99, it does not include 'diff' value, so we do +1, so 'diff + 1' won't be included, but just 'diff' value will be.
return l + r; //add the random number that was selected within distance between low and up to the lower limit.
}
Java解决方案:
public static int generateRandom(int low, int up) {
int l = Math.min(low, up);
int u = Math.max(low, up);
int diff = u - l;
int r = (int) Math.floor(Math.random() * (diff + 1)); // '+1' because Math.random() returns 0..0.99, it does not include 'diff' value, so we do +1, so 'diff + 1' won't be included, but just 'diff' value will be.
return l + r;//add the random number that was selected within distance between low and up to the lower limit.
}
var x = 6; // can be any number
var rand = Math.floor(Math.random()*x) + 1;
TL;博士
function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max - min + 1))
}
要获得随机数generateRandomInteger(-20,20);
下面的解释
整数-不是分数的数字;整数
我们需要得到一个随机数,比如最小值和最大值之间的X。十、 min和max都是整数
即最小<=X<=最大
如果我们从方程中减去min,这相当于
0<=(X-最小)<=(最大-最小)
现在,让我们将其与随机数r相乘这是
0<=(X-分钟)*r<=(最大-分钟)*r
现在,让我们把min加回到等式中
最小<=最小+(X-最小)*r<=最小值+(最大-最小)*r
对于任何给定的X,只有当r的范围为[0,1]时,上述方程才满足。对于r的任何其他值,上述方程都不满足。
在此处了解范围[x,y]或(x,y)的更多信息
我们的下一步是找到一个函数,该函数总是产生范围为[0,1]的值
现在,r的范围,即[0,1]与Javascript中的Math.random()函数非常相似。不是吗?
函数的作用是:返回浮点伪随机范围[0,1)内的数字;即从0(含0)到但不包括1(不含)
使用Math.Random()的随机函数0<=r<1
注意,在Math.random()中,左边界是包含的,右边界是排他的。这意味着min+(max-min)*r的取值范围为[min,max)
为了包括我们的右边界,即[min,max],我们将右边界增加1,并将结果下限。
function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max - min + 1))
}
要获得随机数
generateRandomInteger(-20,20);