I am writing a code which generates 5 random numbers (n1,n2,n3,n4,n5) such that 1<=n1<=4, 1<=n2<=6, 1<=n3<=8, 1<=n4<=12, 1<=n5<=20,
except these combinations:
[1, 1, 1, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 1, 3], [1, 1, 1, 1, 4], [1, 1, 1, 1, 5],
[1, 1, 1, 2, 2], [1, 1, 1, 2, 3], [1, 1, 2, 2, 2], [1, 2, 2, 2, 2], [2, 2, 2, 2, 2]
approaches i am thinking:
- generate random numbers then check it from the set of invalid combinations, if present generate again.
- generate each number of 5 numbers like below
if ((noOfOnes === 1 && noOfTwos === 3) || (noOfOnes === 2 && noOfTwos === 2) || (noOfTwos === 4)) {
var min = 3;
var num = Math.floor(Math.random() * (upperRanges[t] + 1 - min) + min);
inputNumbers.push(num);
}
approach 1 may take time as the situation may occurs when cosecutive invalid combinations are getting generated.
so any help regarding approach 2, or any other approach you can suggest?
Aucun commentaire:
Enregistrer un commentaire