public class

Random

extends Object
implements Serializable
java.lang.Object
   ↳ java.util.Random
Known Direct Subclasses

Class Overview

This class provides methods that generates pseudo-random numbers of different types, such as int, long, double, and float.

Summary

Public Constructors
Random()
Constructs a random generator with an initial state that is unlikely to be duplicated by a subsequent instantiation.
Random(long seed)
Construct a random generator with the given seed as the initial state.
Public Methods
boolean nextBoolean()
Returns the next pseudo-random, uniformly distributed boolean value generated by this generator.
void nextBytes(byte[] buf)
Modifies the byte array by a random sequence of bytes generated by this random number generator.
double nextDouble()
Generates a normally distributed random double number between 0.0 inclusively and 1.0 exclusively.
float nextFloat()
Generates a normally distributed random float number between 0.0 inclusively and 1.0 exclusively.
synchronized double nextGaussian()
Pseudo-randomly generates (approximately) a normally distributed double value with mean 0.0 and a standard deviation value of 1.0 using the polar method of G.
int nextInt(int n)
Returns a new pseudo-random int value which is uniformly distributed between 0 (inclusively) and the value of n (exclusively).
int nextInt()
Generates a uniformly distributed 32-bit int value from the random number sequence.
long nextLong()
Generates a uniformly distributed 64-bit integer value from the random number sequence.
synchronized void setSeed(long seed)
Modifies the seed a using linear congruential formula presented in The Art of Computer Programming, Volume 2, Section 3.2.1.
Protected Methods
synchronized int next(int bits)
Returns a pseudo-random uniformly distributed int value of the number of bits specified by the argument bits as described by Donald E.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Random ()

Since: API Level 1

Constructs a random generator with an initial state that is unlikely to be duplicated by a subsequent instantiation.

The initial state (that is, the seed) is partially based on the current time of day in milliseconds.

See Also

public Random (long seed)

Since: API Level 1

Construct a random generator with the given seed as the initial state. Equivalent to Random r = new Random(); r.setSeed(seed);.

Parameters
seed the seed that will determine the initial state of this random number generator.
See Also

Public Methods

public boolean nextBoolean ()

Since: API Level 1

Returns the next pseudo-random, uniformly distributed boolean value generated by this generator.

Returns
  • a pseudo-random, uniformly distributed boolean value.

public void nextBytes (byte[] buf)

Since: API Level 1

Modifies the byte array by a random sequence of bytes generated by this random number generator.

Parameters
buf non-null array to contain the new random bytes.
See Also

public double nextDouble ()

Since: API Level 1

Generates a normally distributed random double number between 0.0 inclusively and 1.0 exclusively.

Returns
  • a random double in the range [0.0 - 1.0)
See Also

public float nextFloat ()

Since: API Level 1

Generates a normally distributed random float number between 0.0 inclusively and 1.0 exclusively.

Returns
  • float a random float number between [0.0 and 1.0)
See Also

public synchronized double nextGaussian ()

Since: API Level 1

Pseudo-randomly generates (approximately) a normally distributed double value with mean 0.0 and a standard deviation value of 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.

Returns
  • a random double
See Also

public int nextInt (int n)

Since: API Level 1

Returns a new pseudo-random int value which is uniformly distributed between 0 (inclusively) and the value of n (exclusively).

Parameters
n the exclusive upper border of the range [0 - n).
Returns
  • a random int.

public int nextInt ()

Since: API Level 1

Generates a uniformly distributed 32-bit int value from the random number sequence.

Returns
  • a uniformly distributed int value.

public long nextLong ()

Since: API Level 1

Generates a uniformly distributed 64-bit integer value from the random number sequence.

Returns
  • 64-bit random integer.

public synchronized void setSeed (long seed)

Since: API Level 1

Modifies the seed a using linear congruential formula presented in The Art of Computer Programming, Volume 2, Section 3.2.1.

Parameters
seed the seed that alters the state of the random number generator.

Protected Methods

protected synchronized int next (int bits)

Since: API Level 1

Returns a pseudo-random uniformly distributed int value of the number of bits specified by the argument bits as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

Parameters
bits number of bits of the returned value.
Returns
  • a pseudo-random generated int number.