/** * Returns the next pseudorandom, uniformly distributed {@code long} * value from this random number generator's sequence. All * 2<font size="-1"><sup>64</sup></font> possible {@code long} values * should be produced with (approximately) equal probability. * <p> * The default implementation returns * <pre> * <code>(long) (nextDouble() * Long.MAX_VALUE)</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code long} *value from this random number generator's sequence */ public long nextLong() { return (long) ((2d * nextDouble() - 1d) * Long.MAX_VALUE); }
/** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. * <p> * The default implementation fills the array with bytes extracted from * random integers generated using {@link #nextInt}.</p> * * @param bytes the non-null byte array in which to put the * random bytes */ 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; } bytes[bytesOut++] = (byte) randInt; if (bytesOut == bytes.length) { return; } } } }
/** {@inheritDoc} */ public void setSeed(int[] seed) { // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) final long prime = 4294967291l; long combined = 0l; for (int s : seed) { combined = combined * prime + s; } setSeed(combined); }
/** * Returns the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this random * number generator's sequence. * <p> * The default implementation returns * <pre> * <code>(float) nextDouble() </code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this * random number generator's sequence */ public float nextFloat() { return (float) nextDouble(); }
/** {@inheritDoc} */ public void setSeed(int seed) { setSeed((long) seed); }
/** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. * <p> * The default implementation fills the array with bytes extracted from * random integers generated using {@link #nextInt}.</p> * * @param bytes the non-null byte array in which to put the * random bytes */ 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; } bytes[bytesOut++] = (byte) randInt; if (bytesOut == bytes.length) { return; } } } }
/** * 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 * should be produced with (approximately) equal probability. * <p> * The default implementation provided here returns * <pre> * <code>(int) (nextDouble() * Integer.MAX_VALUE)</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence */ public int nextInt() { return (int) ((2d * nextDouble() - 1d) * Integer.MAX_VALUE); }
/** {@inheritDoc} */ public void setSeed(int[] seed) { // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) final long prime = 4294967291l; long combined = 0l; for (int s : seed) { combined = combined * prime + s; } setSeed(combined); }
/** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. * <p> * The default implementation fills the array with bytes extracted from * random integers generated using {@link #nextInt}.</p> * * @param bytes the non-null byte array in which to put the * random bytes */ 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; } bytes[bytesOut++] = (byte) randInt; if (bytesOut == bytes.length) { return; } } } }
/** * Returns the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence. * <p> * The default implementation returns * <pre> * <code>nextDouble() <= 0.5</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence */ public boolean nextBoolean() { return nextDouble() <= 0.5; }
/** {@inheritDoc} */ public void setSeed(int[] seed) { // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) final long prime = 4294967291l; long combined = 0l; for (int s : seed) { combined = combined * prime + s; } setSeed(combined); }
/** * Returns a pseudorandom, uniformly distributed {@code int} value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * <p> * The default implementation returns * <pre> * <code>(int) (nextDouble() * n</code> * </pre></p> * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed {@code int} * value between 0 (inclusive) and n (exclusive). * @throws NotStrictlyPositiveException if {@code n <= 0}. */ public int nextInt(int n) { if (n <= 0 ) { throw new NotStrictlyPositiveException(n); } int result = (int) (nextDouble() * n); return result < n ? result : n - 1; }
/** {@inheritDoc} */ public void setSeed(int seed) { setSeed((long) seed); }
double s = 1; while (s >=1 ) { v1 = 2 * nextDouble() - 1; v2 = 2 * nextDouble() - 1; s = v1 * v1 + v2 * v2;
/** {@inheritDoc} */ public void setSeed(int seed) { setSeed((long) seed); }
/** * Returns the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this random * number generator's sequence. * <p> * The default implementation returns * <pre> * <code>(float) nextDouble() </code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this * random number generator's sequence */ public float nextFloat() { return (float) nextDouble(); }
/** * Returns the next pseudorandom, uniformly distributed {@code long} * value from this random number generator's sequence. All * 2<font size="-1"><sup>64</sup></font> possible {@code long} values * should be produced with (approximately) equal probability. * <p> * The default implementation returns * <pre> * <code>(long) (nextDouble() * Long.MAX_VALUE)</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code long} *value from this random number generator's sequence */ public long nextLong() { return (long) ((2d * nextDouble() - 1d) * Long.MAX_VALUE); }
/** * 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 * should be produced with (approximately) equal probability. * <p> * The default implementation provided here returns * <pre> * <code>(int) (nextDouble() * Integer.MAX_VALUE)</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence */ public int nextInt() { return (int) ((2d * nextDouble() - 1d) * Integer.MAX_VALUE); }
/** * Returns the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence. * <p> * The default implementation returns * <pre> * <code>nextDouble() <= 0.5</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence */ public boolean nextBoolean() { return nextDouble() <= 0.5; }
/** * Returns the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence. * <p> * The default implementation returns * <pre> * <code>nextDouble() <= 0.5</code> * </pre></p> * * @return the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence */ public boolean nextBoolean() { return nextDouble() <= 0.5; }