/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. */ public void nextBytes(byte[] bytes) { nextBytesFill(bytes, 0, bytes.length); }
/** {@inheritDoc} */ public double nextGaussian() { final double random; if (Double.isNaN(nextGaussian)) { // generate a new pair of gaussian numbers final double x = nextDouble(); final double y = nextDouble(); final double alpha = 2 * FastMath.PI * x; final double r = FastMath.sqrt(-2 * FastMath.log(y)); random = r * FastMath.cos(alpha); nextGaussian = r * FastMath.sin(alpha); } else { // use the second element of the pair already generated random = nextGaussian; nextGaussian = Double.NaN; } return random; }
public void setup(final BenchmarkParams b) throws IOException { keySize = intKey ? BYTES : STRING_KEY_LENGTH; crc = new CRC32(); final IntHashSet set = new IntHashSet(num); keys = new int[num]; for (int i = 0; i < num; i++) { if (sequential) { keys[i] = i; } else { while (true) { int candidateKey = RND.nextInt(); if (candidateKey < 0) { candidateKey *= -1; } if (!set.contains(candidateKey)) { set.add(candidateKey); keys[i] = candidateKey; break; } } } } rmdir(TMP_BENCH); tmp = create(b, ""); compact = create(b, "-compacted"); }
/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. * @param start Index at which to start inserting the generated bytes. * @param len Number of bytes to insert. * @throws OutOfRangeException if {@code start < 0} or {@code start >= bytes.length}. * @throws OutOfRangeException if {@code len < 0} or {@code len > bytes.length - start}. */ public void nextBytes(byte[] bytes, int start, int len) { if (start < 0 || start >= bytes.length) { throw new OutOfRangeException(start, 0, bytes.length); } if (len < 0 || len > bytes.length - start) { throw new OutOfRangeException(len, 0, bytes.length - start); } nextBytesFill(bytes, start, len); }
/** {@inheritDoc} */ public double nextGaussian() { final double random; if (Double.isNaN(nextGaussian)) { // generate a new pair of gaussian numbers final double x = nextDouble(); final double y = nextDouble(); final double alpha = 2 * Math.PI * x; final double r = Math.sqrt(-2 * Math.log(y)); random = r * Math.cos(alpha); nextGaussian = r * Math.sin(alpha); } else { // use the second element of the pair already generated random = nextGaussian; nextGaussian = Double.NaN; } return random; }
/** {@inheritDoc} */ public boolean nextBoolean() { return next(1) != 0; }
/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. */ public void nextBytes(byte[] bytes) { nextBytesFill(bytes, 0, bytes.length); }
/** {@inheritDoc} */ public double nextGaussian() { final double random; if (Double.isNaN(nextGaussian)) { // generate a new pair of gaussian numbers final double x = nextDouble(); final double y = nextDouble(); final double alpha = 2 * FastMath.PI * x; final double r = FastMath.sqrt(-2 * FastMath.log(y)); random = r * FastMath.cos(alpha); nextGaussian = r * FastMath.sin(alpha); } else { // use the second element of the pair already generated random = nextGaussian; nextGaussian = Double.NaN; } return random; }
/** {@inheritDoc} */ public float nextFloat() { return next(23) * 0x1.0p-23f; }
/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. */ public void nextBytes(byte[] bytes) { nextBytesFill(bytes, 0, bytes.length); }
/** {@inheritDoc} */ public int nextInt() { return next(32); }
/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. * @param start Index at which to start inserting the generated bytes. * @param len Number of bytes to insert. * @throws OutOfRangeException if {@code start < 0} or {@code start >= bytes.length}. * @throws OutOfRangeException if {@code len < 0} or {@code len > bytes.length - start}. */ public void nextBytes(byte[] bytes, int start, int len) { if (start < 0 || start >= bytes.length) { throw new OutOfRangeException(start, 0, bytes.length); } if (len < 0 || len > bytes.length - start) { throw new OutOfRangeException(len, 0, bytes.length - start); } nextBytesFill(bytes, start, len); }
/** * Generates random bytes and places them into a user-supplied array. * * <p> * The array is filled with bytes extracted from random integers. * This implies that the number of random bytes generated may be larger than * the length of the byte array. * </p> * * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. * @param start Index at which to start inserting the generated bytes. * @param len Number of bytes to insert. * @throws OutOfRangeException if {@code start < 0} or {@code start >= bytes.length}. * @throws OutOfRangeException if {@code len < 0} or {@code len > bytes.length - start}. */ public void nextBytes(byte[] bytes, int start, int len) { if (start < 0 || start >= bytes.length) { throw new OutOfRangeException(start, 0, bytes.length); } if (len < 0 || len > bytes.length - start) { throw new OutOfRangeException(len, 0, bytes.length - start); } nextBytesFill(bytes, start, len); }
/** * Returns a pseudorandom, uniformly distributed {@code long} value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed {@code long} * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ public long nextLong(long n) throws IllegalArgumentException { if (n > 0) { long bits; long val; do { bits = ((long) next(31)) << 32; bits |= ((long) next(32)) & 0xffffffffL; val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); }
/** * {@inheritDoc} * <p>This default implementation is copied from Apache Harmony * java.util.Random (r929253).</p> * * <p>Implementation notes: <ul> * <li>If n is a power of 2, this method returns * {@code (int) ((n * (long) next(31)) >> 31)}.</li> * * <li>If n is not a power of 2, what is returned is {@code next(31) % n} * with {@code next(31)} values rejected (i.e. regenerated) until a * value that is larger than the remainder of {@code Integer.MAX_VALUE / n} * is generated. Rejection of this initial segment is necessary to ensure * a uniform distribution.</li></ul></p> */ public int nextInt(int n) throws IllegalArgumentException { if (n > 0) { if ((n & -n) == n) { return (int) ((n * (long) next(31)) >> 31); } int bits; int val; do { bits = next(31); val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); }