我想要一些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);
}
答案 0 :(得分:5)
此代码将创建四个总和,这些整数总和为最大数字且不为零
var max = 36;
var r1 = randombetween(1, max-3);
var r2 = randombetween(1, max-2-r1);
var r3 = randombetween(1, max-1-r1-r2);
var r4 = max - r1 - r2 - r3;
function randombetween(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
编辑:这个将创建thecount
个整数,总计为max
并将它们返回一个数组(使用上面的randombetween
函数)
function generate(max, thecount) {
var r = [];
var currsum = 0;
for(var i=0; i<thecount-1; i++) {
r[i] = randombetween(1, max-(thecount-i-1)-currsum);
currsum += r[i];
}
r[thecount-1] = max - currsum;
return r;
}
答案 1 :(得分:1)
试试这个:
答案 2 :(得分:1)
对于我自己的项目,我找到了另一种可能对其他人有帮助的解决方案。
我的想法是让所有数字具有相同的概率......我想到的第一件事就是创建一个长度为[1,2,3,..,N,undefined,undefined,....]
的数组max
,将其改组,然后获得1,2,3,...,N
indexOf
N
,但大数字的速度很慢。
最后我找到了另一种我认为正确的解决方案:创建0到1之间的function generate(max, thecount) {
var r = [];
var currsum = 0;
for(var i=0; i<thecount; i++) {
r.push(Math.random());
currsum += r[i];
}
for(var i=0; i<r.length; i++) {
r[i] = Math.round(r[i] / currsum * max);
}
return r;
}
个随机数,这是分数的一部分&#34;他们想要采取&#34;。如果所有数字都选择相同的数字(p.e.1),它们将获得相同的值。
代码,基于@ devnull69的解决方案:
7.4 3.4 5.7 3.3
编辑:使用Math.round解决此解决方案存在问题。想象一下,我们想要4个数字加起来,并且在做Math.round之前得到这个数字:
7 3 6 3
如果你添加它们,它们总和20.但是如果你应用Math.round:
function generate(max, thecount) {
var r = [];
var decimals = [];
var currsum = 0;
for(var i=0; i<thecount; i++) {
r.push(Math.random());
currsum += r[i];
}
var remaining = max;
for(var i=0; i<r.length; i++) {
var res = r[i] / currsum * max;
r[i] = Math.floor(res);
remaining -= r[i];
decimals.push(res - r[i]);
}
while(remaining > 0){
var maxPos = 0;
var maxVal = 0;
for(var i=0; i<decimals.length; i++){
if(maxVal < decimals[i]){
maxVal = decimals[i];
maxPos = i;
}
}
r[maxPos]++;
decimals[maxPos] = 0; // We set it to 0 so we don't give this position another one.
remaining--;
}
return r;
}
他们加入了18。
然后在我的项目中,我不得不做另一轮,让那些&#34;失踪&#34;值为具有较高小数的数字。这变得更复杂,如:
notify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
notifyMail.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
notifyMail.setVisibility(View.GONE);
}
});
notifyPhone.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
notifyPhone.setVisibility(View.GONE);
}
});
notifyHitcher.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
notifyHitcher.setVisibility(View.GONE);
}
});
notifyDriver.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
notifyDriver.setVisibility(View.GONE);
}
});
}
else
{
notifyMail.animate().translationY(0);
}
}
});
答案 3 :(得分:0)
没有说生成的随机数应该都是不同的,例如,如果数字的总和应为4,则不可能。
<!DOCTYPE html>
<html>
<head>
<title>Random numbers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function randomSum(sum, nrOfNumbers) {
var a = new Array(nrOfNumbers);
var i = 0;
var remainingSum = sum;
while(i < nrOfNumbers) {
if(i == (nrOfNumbers -1)) {
a[i] = remainingSum;
}
else {
// get a random number in range [0, remainingSum]
a[i] = Math.floor(Math.random() * (remainingSum + 1));
remainingSum -= a[i];
}
++i;
}
return a;
}
</script>
</head>
<body>
<script type="text/javascript">
var randomNumbers = randomSum(20,4);
for(var i in randomNumbers) {
document.writeln(randomNumbers[i]+"<br>");
}
</script>
</body>
</html>
答案 4 :(得分:0)
任何语言的伪代码。
a = rand()
b = rand()
c = rand()
d = rand()
total = a + b + c + d
adjust = desiredNumber / total
a *= adjust
b *= adjust
c *= adjust
d *= adjust