Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Random

public class Random
extends Object implements Serializable

java.lang.Object
   ↳ java.util.Random
Known Direct Subclasses


This class provides methods that return pseudo-random values.

It is dangerous to seed Random with the current time because that value is more predictable to an attacker than the default seed.

This class is thread-safe.

See also:

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 a pseudo-random uniformly distributed boolean.

void nextBytes(byte[] buf)

Fills buf with random bytes.

double nextDouble()

Returns a pseudo-random uniformly distributed double in the half-open range [0.0, 1.0).

float nextFloat()

Returns a pseudo-random uniformly distributed float in the half-open range [0.0, 1.0).

double nextGaussian()

Returns a pseudo-random (approximately) normally distributed double with mean 0.0 and standard deviation 1.0.

int nextInt(int n)

Returns a pseudo-random uniformly distributed int in the half-open range [0, n).

int nextInt()

Returns a pseudo-random uniformly distributed int.

long nextLong()

Returns a pseudo-random uniformly distributed long.

void setSeed(long seed)

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

Protected methods

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.

Inherited methods

From class java.lang.Object

Public constructors

Random

Added in API level 1
Random ()

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

Random

Added in API level 1
Random (long seed)

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

This constructor is mainly useful for predictability in tests. The default constructor is likely to provide better randomness.

Parameters
seed long

Public methods

nextBoolean

Added in API level 1
boolean nextBoolean ()

Returns a pseudo-random uniformly distributed boolean.

Returns
boolean

nextBytes

Added in API level 1
void nextBytes (byte[] buf)

Fills buf with random bytes.

Parameters
buf byte

nextDouble

Added in API level 1
double nextDouble ()

Returns a pseudo-random uniformly distributed double in the half-open range [0.0, 1.0).

Returns
double

nextFloat

Added in API level 1
float nextFloat ()

Returns a pseudo-random uniformly distributed float in the half-open range [0.0, 1.0).

Returns
float

nextGaussian

Added in API level 1
double nextGaussian ()

Returns a pseudo-random (approximately) normally distributed double with mean 0.0 and standard deviation 1.0. This method uses 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
double

nextInt

Added in API level 1
int nextInt (int n)

Returns a pseudo-random uniformly distributed int in the half-open range [0, n).

Parameters
n int
Returns
int

nextInt

Added in API level 1
int nextInt ()

Returns a pseudo-random uniformly distributed int.

Returns
int

nextLong

Added in API level 1
long nextLong ()

Returns a pseudo-random uniformly distributed long.

Returns
long

setSeed

Added in API level 1
void setSeed (long seed)

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

Parameters
seed long

Protected methods

next

Added in API level 1
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. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

Most applications will want to use one of this class' convenience methods instead.

Subclasses only need to override this method to alter the behavior of all the public methods.

Parameters
bits int
Returns
int
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.