# Re: [01/18] [math] MATH-1307 Classic List Threaded 12 messages Open this post in threaded view
|

## Re: [01/18] [math] MATH-1307

> +     * This default implementation is copied from Apache Harmony > +     * java.util.Random (r929253). > +     *

> +     * > +     *

Implementation notes: > +     *

> +     *
• If n is a power of 2, this method returns > +     *    {@code (int) ((n * (long) next(31)) >> 31)}.
• > +     *
• 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.
• > +     *
> +     *

> +     */ > +    @Override > +    public int nextInt(int n) throws IllegalArgumentException { > +        if (n > 0) { > +            if ((n & -n) == n) { > +                return (int) ((n * (long) (nextInt() >>> 1)) >> 31); > +            } > +            int bits; > +            int val; > +            do { > +                bits = (nextInt() >>> 1); > +                val = bits % n; > +            } while (bits - val + (n - 1) < 0); > +            return val; > +        } > + > +        throw new NotStrictlyPositiveException(n); > +    } > + > +    /** {@inheritDoc} */ > +    @Override > +    public long nextLong() { > +        final long high  = ((long) nextInt()) << 32; > +        final long low  = nextInt() & 0xffffffffL; > +        return high | low; > +    } > + > +    /** > +     * 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) { > +        if (n > 0) { > +            long bits; > +            long val; > +            do { > +                bits = ((long) (nextInt() >>> 1)) << 32; > +                bits |= ((long) nextInt()) & 0xffffffffL; > +                val  = bits % n; > +            } while (bits - val + (n - 1) < 0); > +            return val; > +        } > + > +        throw new NotStrictlyPositiveException(n); > +    } > + > +    /** > +     * Clears the cache used by the default implementation of > +     * {@link #nextGaussian}. > +     */ > +    public void clear() { > +        nextGaussian = Double.NaN; > +    } > + > +    /** > +     * Generates random bytes and places them into a user-supplied array. > +     * > +     *

> +     * The array is filled with bytes extracted from random integers generated > +     * using {@link #nextInt()}. > +     * This implies that the number of random bytes generated may be larger than > +     * the length of the byte array. > +     *

> +     * > +     * @param bytes Array in which to put the generated bytes. Cannot be {@code null}. > +     */ > +    @Override > +    public void nextBytes(byte[] bytes) { > +        nextBytesFill(bytes, 0, bytes.length); > +    } > + > +    /** > +     * Generates random bytes and places them into a user-supplied array. > +     * > +     *

> +     * The array is filled with bytes extracted from random integers generated > +     * using {@link #nextInt()}. > +     * This implies that the number of random bytes generated may be larger than > +     * the length of the byte array. > +     *

> +     * > +     * @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); > +        } > +        final int max = bytes.length - start; > +        if (len <= 0 || > +            len > max) { > +            throw new OutOfRangeException(len, 0, max); > +        } > + > +        nextBytesFill(bytes, start, len); > +    } > + > +    /** > +     * Generates random bytes and places them into a user-supplied array. > +     * > +     *

> +     * The array is filled with bytes extracted from random integers generated > +     * using {@link #nextInt()}. > +     * This implies that the number of random bytes generated may be larger than > +     * the length of the byte array. > +     *

Open this post in threaded view
|

## [Math] About the refactoring of RNGs (Was: [01/18] [math] MATH-1307)

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs (Was: [01/18] [math] MATH-1307)

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs (Was: [01/18] [math] MATH-1307)

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs (Was: [01/18] [math] MATH-1307)

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs (Was: [01/18] [math] MATH-1307)

Open this post in threaded view
|

## Re: [Math] About the refactoring of RNGs

Open this post in threaded view
|