8188047: Add SplittableRandom.nextBytes

Reviewed-by: martin, psandoz
This commit is contained in:
Doug Lea 2017-10-13 18:19:18 -07:00
parent 925bc58717
commit f8ae408aa9
4 changed files with 123 additions and 1 deletions

View file

@ -398,6 +398,26 @@ public final class SplittableRandom {
return new SplittableRandom(nextLong(), mixGamma(nextSeed()));
}
/**
* Fills a user-supplied byte array with generated pseudorandom bytes.
*
* @param bytes the byte array to fill with pseudorandom bytes
* @throws NullPointerException if bytes is null
* @since 10
*/
public void nextBytes(byte[] bytes) {
int i = 0;
int len = bytes.length;
for (int words = len >> 3; words--> 0; ) {
long rnd = nextLong();
for (int n = 8; n--> 0; rnd >>>= Byte.SIZE)
bytes[i++] = (byte)rnd;
}
if (i < len)
for (long rnd = nextLong(); i < len; rnd >>>= Byte.SIZE)
bytes[i++] = (byte)rnd;
}
/**
* Returns a pseudorandom {@code int} value.
*

View file

@ -1039,7 +1039,10 @@ public class ThreadLocalRandom extends Random {
*/
private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL;
// Constants from SplittableRandom
/**
* The least non-zero value returned by nextDouble(). This value
* is scaled by a random value of 53 bits to produce a result.
*/
private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53)
private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24)