/** Returns a pseudo-random, uniformly distributed {@code float} value between 0.0 and 1.0 from this random number generator's * sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public float nextFloat () { return (float)((nextLong() >>> 40) * NORM_FLOAT); }
/** Returns a pseudo-random, uniformly distributed {@code double} value between 0.0 and 1.0from this random number generator's * sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public double nextDouble () { return (nextLong() >>> 11) * NORM_DOUBLE; }
/** Returns the next pseudo-random, uniformly distributed {@code int} value from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public int nextInt () { return (int)nextLong(); }
/** Returns a pseudo-random, uniformly distributed {@code int} value between 0 (inclusive) and the specified value (exclusive), * drawn from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. * @param n the positive bound on the random number to be returned. * @return the next pseudo-random {@code int} value between {@code 0} (inclusive) and {@code n} (exclusive). */ @Override public int nextInt (final int n) { return (int)nextLong(n); }
/** This protected method is final because, contrary to the superclass, it's not used anymore by the other methods. */ @Override protected final int next (int bits) { return (int)(nextLong() & ((1L << bits) - 1)); }
/** Returns a pseudo-random, uniformly distributed {@code boolean } value from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public boolean nextBoolean () { return (nextLong() & 1) != 0; }
/** 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> * This implementation uses {@link #nextLong()} internally. */ @Override public void nextBytes (final byte[] bytes) { int n = 0; int i = bytes.length; while (i != 0) { n = i < 8 ? i : 8; // min(i, 8); for (long bits = nextLong(); n-- != 0; bits >>= 8) bytes[--i] = (byte)bits; } }
/** Returns a pseudo-random, uniformly distributed {@code long} value between 0 (inclusive) and the specified value (exclusive), * drawn from this random number generator's sequence. The algorithm used to generate the value guarantees that the result is * uniform, provided that the sequence of 64-bit values produced by this generator is. * <p> * This implementation uses {@link #nextLong()} internally. * @param n the positive bound on the random number to be returned. * @return the next pseudo-random {@code long} value between {@code 0} (inclusive) and {@code n} (exclusive). */ public long nextLong (final long n) { if (n <= 0) throw new IllegalArgumentException("n must be positive"); for (;;) { final long bits = nextLong() >>> 1; final long value = bits % n; if (bits - value + (n - 1) >= 0) return value; } }
/** This protected method is final because, contrary to the superclass, it's not used anymore by the other methods. */ @Override protected final int next (int bits) { return (int)(nextLong() & ((1L << bits) - 1)); }
/** Returns a pseudo-random, uniformly distributed {@code int} value between 0 (inclusive) and the specified value (exclusive), * drawn from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. * @param n the positive bound on the random number to be returned. * @return the next pseudo-random {@code int} value between {@code 0} (inclusive) and {@code n} (exclusive). */ @Override public int nextInt (final int n) { return (int)nextLong(n); }
/** Returns a pseudo-random, uniformly distributed {@code double} value between 0.0 and 1.0from this random number generator's * sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public double nextDouble () { return (nextLong() >>> 11) * NORM_DOUBLE; }
/** Returns a pseudo-random, uniformly distributed {@code boolean } value from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public boolean nextBoolean () { return (nextLong() & 1) != 0; }
/** Returns a pseudo-random, uniformly distributed {@code float} value between 0.0 and 1.0 from this random number generator's * sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public float nextFloat () { return (float)((nextLong() >>> 40) * NORM_FLOAT); }
/** Returns the next pseudo-random, uniformly distributed {@code int} value from this random number generator's sequence. * <p> * This implementation uses {@link #nextLong()} internally. */ @Override public int nextInt () { return (int)nextLong(); }
/** 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> * This implementation uses {@link #nextLong()} internally. */ @Override public void nextBytes (final byte[] bytes) { int n = 0; int i = bytes.length; while (i != 0) { n = i < 8 ? i : 8; // min(i, 8); for (long bits = nextLong(); n-- != 0; bits >>= 8) bytes[--i] = (byte)bits; } }
/** Returns a pseudo-random, uniformly distributed {@code long} value between 0 (inclusive) and the specified value (exclusive), * drawn from this random number generator's sequence. The algorithm used to generate the value guarantees that the result is * uniform, provided that the sequence of 64-bit values produced by this generator is. * <p> * This implementation uses {@link #nextLong()} internally. * @param n the positive bound on the random number to be returned. * @return the next pseudo-random {@code long} value between {@code 0} (inclusive) and {@code n} (exclusive). */ public long nextLong (final long n) { if (n <= 0) throw new IllegalArgumentException("n must be positive"); for (;;) { final long bits = nextLong() >>> 1; final long value = bits % n; if (bits - value + (n - 1) >= 0) return value; } }