Demonstrating a but in java.util.Random

This commit is contained in:
marcin.sciesinski
2019-09-20 12:59:56 -07:00
parent bede97baf3
commit 34b71d06d7

View File

@@ -79,19 +79,22 @@ public class HobbitDraftTest {
}
private static void testRandomness() {
doRandomTest(false);
doRandomTest(4, false);
doRandomTest(3, false);
System.out.println("WTF!!!!");
doRandomTest(true);
doRandomTest(4, true);
doRandomTest(3, true);
}
private static void doRandomTest(boolean getFloatBeforeInt) {
private static void doRandomTest(int nextIntValue, boolean getFloatBeforeInt) {
System.out.println("Get float before int: " + getFloatBeforeInt);
System.out.println("Next int value: " + nextIntValue);
int[] values = new int[4];
for (int i = 0; i < 1000; i++) {
Random rnd = new Random(i);
if (getFloatBeforeInt)
rnd.nextFloat();
values[rnd.nextInt(4)]++;
values[rnd.nextInt(nextIntValue)]++;
}
for (int i = 0; i < values.length; i++) {
System.out.println(i + ": " + values[i]);