Reputation: 111
I need to select a value based on a percentage chance of that value being selected. For example:
The percentages will always add up to exactly 100%
I have encountered several solutions like this one, but have determined that they cannot possibly be correct. Here is a sample program built using the solution mentioned:
import java.util.Random;
public class Main {
private static Random r = new Random();
public static void main(String[] args) {
final int iterations = 1000000;
System.out.println("Testing percentage based random, " + iterations + " iterations");
int onePercent = 0;
int sixPercent = 0;
int sevenPercent = 0;
int thirtySixPercent = 0;
int fiftyPercent = 0;
// Those values add up to 100% overall
for (int i = 0; i < iterations; i++) {
int random = r.nextInt(100);
if (random < 1) {
onePercent++;
continue;
}
if (random < 6) {
sixPercent++;
continue;
}
if (random < 7) {
sevenPercent++;
continue;
}
if (random < 36) {
thirtySixPercent++;
continue;
}
if (random < 50) {
fiftyPercent++;
continue;
}
// That can't be right because if random > 50 then nothing at all happens
}
System.out.println("One percent happened about " + (onePercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Six percent happened about " + (sixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Seven percent happened about " + (sevenPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Thirty six percent happened about " + (thirtySixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Fifty percent happened about " + (fiftyPercent / Float.valueOf(iterations)) * 100 + "% of the time");
}
}
Output:
Testing percentage based random, 1000000 iterations
One percent happened about 0.99649996% of the time
Six percent happened about 4.9925% of the time
Seven percent happened about 1.0029999% of the time
Thirty six percent happened about 29.001299% of the time
Fifty percent happened about 14.0191% of the time
Expected output:
Testing percentage based random, 1000000 iterations
One percent happened about 0.99649996% of the time
Six percent happened about 6.9925% of the time
Seven percent happened about 7.0029999% of the time
Thirty six percent happened about 36.001299% of the time
Fifty percent happened about 50.0191% of the time
I believe I need to use some sort of algorithm to convert the percentages into a scale from 0 to 99 so that the random number generator can select a value accurately. I cannot think of how to do that, though.
Upvotes: 1
Views: 572
Reputation: 111
Figured it out. You need to keep track of the percentage tested so far and add it to the current test.
import java.util.Random;
public class Main {
private static Random r = new Random();
public static void main(String[] args) {
final int iterations = 1000000;
System.out.println("Testing percentage based random, " + iterations + " iterations");
int onePercent = 0;
int sixPercent = 0;
int sevenPercent = 0;
int thirtySixPercent = 0;
int fiftyPercent = 0;
// Those values add up to 100% overall
for (int i = 0; i < iterations; i++) {
int random = r.nextInt(100);
int totalPercent = 0;
if (random < totalPercent + 1) {
onePercent++;
continue;
}
totalPercent += 1;
if (random < totalPercent + 6) {
sixPercent++;
continue;
}
totalPercent += 6;
if (random < totalPercent + 7) {
sevenPercent++;
continue;
}
totalPercent += 7;
if (random < totalPercent + 36) {
thirtySixPercent++;
continue;
}
totalPercent += 36;
if (random < totalPercent + 50) {
fiftyPercent++;
continue;
}
totalPercent += 50;
// That can't be right because if random > 50 then nothing at all happens
}
System.out.println("One percent happened about " + (onePercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Six percent happened about " + (sixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Seven percent happened about " + (sevenPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Thirty six percent happened about " + (thirtySixPercent / Float.valueOf(iterations)) * 100 + "% of the time");
System.out.println("Fifty percent happened about " + (fiftyPercent / Float.valueOf(iterations)) * 100 + "% of the time");
}
}
Upvotes: -1
Reputation: 2989
Your results are correct :
Fifty percent happened about 14.0191% of the time
50 - 36 = 14
Thirty six percent happened about 29.001299% of the time
36 - 7 = 29
Seven percent happened about 1.0029999% of the time
7 - 6 = 1
....
Delete all 'continue' statements if you want them to sum up.
Upvotes: 1