Sometimes my test code can look very weird when I am attempting to figure something out.
Can you guess what this code block is used to test for ?
int num1 = 50;
int num2 = 75;
int num3 = 100;
int num4 = 125;
int catche = 0;
List<Integer> dump = new ArrayList<Integer>();
List<Integer> low = new ArrayList<Integer>();
List<Integer> high = new ArrayList<Integer>();
dump.add(num1);
low.add(catche + 1);
catche += num1;
high.add(catche);
dump.add(num2);
low.add(catche + 1);
catche += num2;
high.add(catche);
dump.add(num3);
low.add(catche + 1);
catche += num3;
high.add(catche);
dump.add(num4);
low.add(catche + 1);
catche += num4;
high.add(catche);
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int cnt = 0;
while ( cnt != 1001){
int temp_rand = rand.nextInt(catche);
for (int i = 0; i < dump.size(); i ++ ){
if (temp_rand >= low.get(i) && temp_rand <= high.get(i) ){
switch (i){
case 0: one ++; break;
case 1: two ++; break;
case 2: three ++; break;
case 3: four ++; break;
default:break;
}
//say("\n" + dump.get(i));
}
}
cnt ++;
}
say("One: " + (int)(((double)one / cnt)* 100) + "% Two: " + (int)(((double)two / cnt)* 100) + "% Three: " +
(int)(((double)three / cnt)* 100) + "% Four: " + (int)(((double)four / cnt)* 100) + "%" );
public void say(String x){
System.out.println(x);
}
The answer is: determining probability by weight
Explained -
One object must be picked out of a list of objects, but each object has a different "weight" ( likelihood ) of being picked.
Real Life Example -
You have 5 red marbles.
Your friend has 10 green marbles.
Your second friend has 15 yellow marbles.
Your third friend has 20 blue marbles.
If you put them all in a bag, and pulled out just one, which color would likely be picked?
What is the chance for each color ?