Does it have to be a SecureRandom
?
If not, then you have two easy options, using nextLong(long bound)
of SplittableRandom
or ThreadLocalRandom
:
long nextLong = new SplittableRandom().nextLong(4294967296L);
long nextLong = ThreadLocalRandom.current().nextLong(4294967296L);
UPDATE
If it has to be SecureRandom
, you can always copy the code of those two methods, which are implemented the same (below copied from Java 11):
public static long nextLong(SecureRandom random, long bound) {
if (bound <= 0)
throw new IllegalArgumentException(BadBound);
long r = random.nextLong();
long m = bound - 1;
if ((bound & m) == 0L) // power of two
r &= m;
else { // reject over-represented candidates
for (long u = r >>> 1;
u + m - (r = u % bound) < 0L;
u = random.nextLong() >>> 1)
;
}
return r;
}
That should answer the question "Is there a work around to this?", since writing your own code is a valid workaround.