我想要一些 javascript,它可以让我生成 4 个随机数,这些随机数加起来为某个值,例如
如果
max = 20
然后
num1 = 4
num2 = 4
num3 = 7
num4 = 5
或者
max = 36
然后
num1 = 12
num2 = 5
num3 = 9
num4 = 10
到目前为止我所拥有的是...
var maxNum = 20;
var quarter;
var lowlimit;
var upplimit;
var num1 = 1000;
var num2 = 1000;
var num3 = 1000;
var num4 = 1000;
var sumnum = num1+num2+num3+num4;
quarter = maxNum * 0.25;
lowlimit = base - (base * 0.5);
upplimit = base + (base * 0.5);
if(sumnum != maxNum){
num1 = Math.floor(Math.random()*(upplimit-lowlimit+1)+lowlimit);
num2 = Math.floor(Math.random()*(upplimit-lowlimit+1)+lowlimit);
num3 = Math.floor(Math.random()*(upplimit-lowlimit+1)+lowlimit);
num4 = Math.floor(Math.random()*(upplimit-lowlimit+1)+lowlimit);
}