MATH-1304 Replaced implementation of "nextBytes" by copying the code from "BitsStreamGenerator" class (and then replacing the call to "next(32)" by "nextInt()").
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/813aa11d Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/813aa11d Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/813aa11d Branch: refs/heads/master Commit: 813aa11d505b9507b6795d36070806cb2d96e5c2 Parents: 2d86841 Author: Gilles <[email protected]> Authored: Mon Dec 21 01:26:18 2015 +0100 Committer: Gilles <[email protected]> Committed: Mon Dec 21 01:26:18 2015 +0100 ---------------------------------------------------------------------- .../math4/random/AbstractRandomGenerator.java | 30 +++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/813aa11d/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java b/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java index 173f4ee..c31dc68 100644 --- a/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java +++ b/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java @@ -107,24 +107,26 @@ public abstract class AbstractRandomGenerator implements RandomGenerator { */ @Override public void nextBytes(byte[] bytes) { - int bytesOut = 0; - while (bytesOut < bytes.length) { - int randInt = nextInt(); - for (int i = 0; i < 3; i++) { - if (i > 0) { - randInt >>= 8; - } - } - if (bytesOut < bytes.length) { - bytes[bytesOut++] = (byte) randInt; - if (bytesOut == bytes.length) { - return; - } + int i = 0; + final int iEnd = bytes.length - 3; + while (i < iEnd) { + final int random = nextInt(); + bytes[i] = (byte) (random & 0xff); + bytes[i + 1] = (byte) ((random >> 8) & 0xff); + bytes[i + 2] = (byte) ((random >> 16) & 0xff); + bytes[i + 3] = (byte) ((random >> 24) & 0xff); + i += 4; + } + if (i < bytes.length) { + int random = nextInt(); + while (i < bytes.length) { + bytes[i++] = (byte) (random & 0xff); + random >>= 8; } } } - /** + /** * Returns the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence. * All 2<font size="-1"><sup>32</sup></font> possible {@code int} values
