8215441: Increase uniformity of the distribution of BigIntegers constructed by BigInteger(int, Random)
Douglas Surber
douglas.surber at oracle.com
Thu Dec 20 17:18:44 UTC 2018
I wrote the following simple test case to look at the uniformity of the distribution. I don't see any problem running it up to 4096 buckets. Admittedly I did not do any statistical tests on the buckets but by eye they look uniformly distributed.
public static void main(String[] args) throws Throwable {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
final int nBits = 4096;
final int nBuckets = 128;
final int count = 100000;
Pair[] buckets = new Pair[nBuckets];
BigInteger max = BigInteger.TWO.pow(nBits);
BigInteger limit = max;
BigInteger step = limit.divide(BigInteger.valueOf(buckets.length));
for (int i = 0; i < buckets.length; i++) buckets[i] = new Pair(limit = limit.subtract(step));
for (int i = 0; i < count; ++i) {
// biased towards high numbers. never chooses below a high limit
BigInteger number = new BigInteger(nBits, sr);
int j;
for (j = 0; buckets[j].limit.compareTo(number) > 0; j++) {}
buckets[j].count++;
}
for (int i = buckets.length; i > 0; i--) System.out.print(buckets[i-1].count + (i%8==0 ? "\n" : "\t"));
System.out.println();
}
Douglas
More information about the core-libs-dev
mailing list