FileDocCategorySizeDatePackage
Random.javaAPI DocAndroid 1.5 API8268Wed May 06 22:41:04 BST 2009java.util

Random

public class Random extends Object implements Serializable
This class provides methods that generates pseudo-random numbers of different types, such as {@code int}, {@code long}, {@code double}, and {@code float}.
see
Properties
see
PropertyResourceBundle
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
static final long
multiplier
boolean
haveNextNextGaussian
The boolean value indicating if the second Gaussian number is available.
long
seed
It is associated with the internal state of this generator.
double
nextNextGaussian
The second Gaussian generated number.
Constructors Summary
public Random()
Construct a random generator with the current time of day in milliseconds as the initial state.

see
#setSeed
since
Android 1.0


                               
      
        setSeed(System.currentTimeMillis());
    
public Random(long seed)
Construct a random generator with the given {@code seed} as the initial state.

param
seed the seed that will determine the initial state of this random number generator.
see
#setSeed
since
Android 1.0

        setSeed(seed);
    
Methods Summary
protected synchronized intnext(int bits)
Returns a pseudo-random uniformly distributed {@code int} value of the number of bits specified by the argument {@code bits} as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

param
bits number of bits of the returned value.
return
a pseudo-random generated int number.
since
Android 1.0
see
#nextBytes
see
#nextDouble
see
#nextFloat
see
#nextInt()
see
#nextInt(int)
see
#nextGaussian
see
#nextLong
since
Android 1.0

        seed = (seed * multiplier + 0xbL) & ((1L << 48) - 1);
        return (int) (seed >>> (48 - bits));
    
public booleannextBoolean()
Returns the next pseudo-random, uniformly distributed {@code boolean} value generated by this generator.

return
a pseudo-random, uniformly distributed boolean value.
since
Android 1.0

        return next(1) != 0;
    
public voidnextBytes(byte[] buf)
Modifies the {@code byte} array by a random sequence of {@code byte}s generated by this random number generator.

param
buf non-null array to contain the new random {@code byte}s.
see
#next
since
Android 1.0

        int rand = 0, count = 0, loop = 0;
        while (count < buf.length) {
            if (loop == 0) {
                rand = nextInt();
                loop = 3;
            } else {
                loop--;
            }
            buf[count++] = (byte) rand;
            rand >>= 8;
        }
    
public doublenextDouble()
Generates a normally distributed random {@code double} number between 0.0 inclusively and 1.0 exclusively.

return
a random {@code double} in the range [0.0 - 1.0)
see
#nextFloat
since
Android 1.0

        return ((((long) next(26) << 27) + next(27)) / (double) (1L << 53));
    
public floatnextFloat()
Generates a normally distributed random {@code float} number between 0.0 inclusively and 1.0 exclusively.

return
float a random {@code float} number between [0.0 and 1.0)
see
#nextDouble
since
Android 1.0

        return (next(24) / 16777216f);
    
public synchronized doublenextGaussian()
Pseudo-randomly generates (approximately) a normally distributed {@code double} value with mean 0.0 and a standard deviation value of {@code 1.0} using the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P.

return
a random {@code double}
see
#nextDouble
since
Android 1.0

        if (haveNextNextGaussian) { // if X1 has been returned, return the
                                    // second Gaussian
            haveNextNextGaussian = false;
            return nextNextGaussian;
        }
        
        double v1, v2, s;
        do {
            v1 = 2 * nextDouble() - 1; // Generates two independent random
                                        // variables U1, U2
            v2 = 2 * nextDouble() - 1;
            s = v1 * v1 + v2 * v2;
        } while (s >= 1);
        double norm = Math.sqrt(-2 * Math.log(s) / s);
        nextNextGaussian = v2 * norm; // should that not be norm instead
                                        // of multiplier ?
        haveNextNextGaussian = true;
        return v1 * norm; // should that not be norm instead of multiplier
                            // ?
    
public intnextInt(int n)
Returns a new pseudo-random {@code int} value which is uniformly distributed between 0 (inclusively) and the value of {@code n} (exclusively).

param
n the exclusive upper border of the range [0 - n).
return
a random {@code int}.
since
Android 1.0

        if (n > 0) {
            if ((n & -n) == n) {
                return (int) ((n * (long) next(31)) >> 31);
            }
            int bits, val;
            do {
                bits = next(31);
                val = bits % n;
            } while (bits - val + (n - 1) < 0);
            return val;
        }
        throw new IllegalArgumentException();
    
public intnextInt()
Generates a uniformly distributed 32-bit {@code int} value from the random number sequence.

return
a uniformly distributed {@code int} value.
see
java.lang.Integer#MAX_VALUE
see
java.lang.Integer#MIN_VALUE
see
#next
see
#nextLong
since
Android 1.0

        return next(32);
    
public longnextLong()
Generates a uniformly distributed 64-bit integer value from the random number sequence.

return
64-bit random integer.
see
java.lang.Integer#MAX_VALUE
see
java.lang.Integer#MIN_VALUE
see
#next
see
#nextInt()
see
#nextInt(int)
since
Android 1.0

        return ((long) next(32) << 32) + next(32);
    
public synchronized voidsetSeed(long seed)
Modifies the seed a using linear congruential formula presented in The Art of Computer Programming, Volume 2, Section 3.2.1.

param
seed the seed that alters the state of the random number generator.
see
#next
see
#Random()
see
#Random(long)
since
Android 1.0

        this.seed = (seed ^ multiplier) & ((1L << 48) - 1);
        haveNextNextGaussian = false;