How to generate a random BigInteger value in Java?
I need to generate arbitrarily large random integers in the range 0 (inclusive) to n (exclusive). My initial thought was to call nextDouble and multiply by n, but once n gets to be larger than 2 53 , the results would no longer be uniformly distributed. BigInteger has the following constructor available: public BigInteger(int numBits, Random rnd) Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2 numBits - 1), inclusive. How can this be used to get a random value in the range 0 - n, where n is not a power of 2? Thomas Pornin Use a loop: BigInteger