I am trying to make this thing where it generates 7 random numbers. I am using
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateNum(max, thecount) {
var r = [];
var currsum = 0;
for (var i = 0; i < thecount - 1; i++) {
r[i] = getRandomInt(15, max - (thecount - i - 1) - currsum);
currsum += r[i];
}
r[thecount - 1] = max - currsum;
return r;
}
This will sometimes return numbers that are NaN
or are greater than 40 (which needs to be the max)
or less than 15 (which needs to be the min) and even less than 0.
It generates numbers that add up to another random number which is somewhere between 110 or 150.
How can I make it add up to the total random number and still be in a certain range?
Aucun commentaire:
Enregistrer un commentaire