I am creating a simple math program with 6 different problem types. I want the program to randomly display one of the 6 types, but some of the problems should appear more often. I use a weighted array, but after the problem type is selected from the weighted array I am having trouble figuring out which problem type it was without using 10 or more "or" commands inside of an if statement. Here is a simplified version:
//shuffle array of 10 integers to get a random value
var rand_10 = [0,1,2,3,4,5,6,7,8,9];
fisherYates(rand_10);
//Set weightedProb
weightedProb[0] = probType[0];
weightedProb[1] = probType[0];
weightedProb[2] = probType[0];
.
.
.
weightedProb[8] = probType[0];
weightedProb[9] = probType[1];
theProblem = weightedProb[rand_10[0]];
if(rand_10[0] == 0 || rand_10[0] == 1 || rand_10[0] == 2 || rand_10[0] == 3 ||rand_10[0] == 4||rand_10[0] == 5||rand_10[0] == 6||rand_10[0] == 7||rand_10[0] == 8){
//do something
}else if(rand_10[0] ==9){
//do something else
}
Would a simple range comparison (>
and <
) be what you are after?
e.g.
if(rand_10[0] >= 0 && rand_10[0] <= 8){
//do something
}else if(rand_10[0] == 9){
//do something else
}