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.
*