I need to get a random BigInteger that is bigger than 2^511 and lower than 2^512.
byte[] bytes = new byte[64]; // 512 bits
new Random().nextBytes(bytes);
bytes[0] |= 0x80; // set the most significant bit
return new BigInteger(1, bytes);
From the doc :
BigInteger(int numBits, Random rnd)
Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.
So something like that should work :
BigInteger number = new BigInteger(512, new Random()); //Give you a number between 0 and 2^512 - 1
number = number.setBit(0); //Set the first bit so number is between 2^511 and 2^512 - 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With