Package java.util
Class Random
- java.lang.Object
-
- java.util.Random
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
SecureRandom,ThreadLocalRandom
public class Random extends Object implements Serializable
This class provides methods that return pseudo-random values.It is dangerous to seed
Randomwith the current time because that value is more predictable to an attacker than the default seed.This class is thread-safe.
- See Also:
SecureRandom, Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intnext(int bits)Returns a pseudo-random uniformly distributedintvalue of the number of bits specified by the argumentbitsas described by Donald E.booleannextBoolean()Returns a pseudo-random uniformly distributedboolean.voidnextBytes(byte[] buf)Fillsbufwith random bytes.doublenextDouble()Returns a pseudo-random uniformly distributeddoublein the half-open range [0.0, 1.0).floatnextFloat()Returns a pseudo-random uniformly distributedfloatin the half-open range [0.0, 1.0).doublenextGaussian()Returns a pseudo-random (approximately) normally distributeddoublewith mean 0.0 and standard deviation 1.0.intnextInt()Returns a pseudo-random uniformly distributedint.intnextInt(int n)Returns a pseudo-random uniformly distributedintin the half-open range [0, n).longnextLong()Returns a pseudo-random uniformly distributedlong.voidsetSeed(long seed)Modifies the seed using a linear congruential formula presented in The Art of Computer Programming, Volume 2, Section 3.2.1.
-
-
-
Constructor Detail
-
Random
public Random()
Constructs a random generator with an initial state that is unlikely to be duplicated by a subsequent instantiation.
-
Random
public Random(long seed)
Construct a random generator with the givenseedas the initial state. Equivalent toRandom r = new Random(); r.setSeed(seed);.This constructor is mainly useful for predictability in tests. The default constructor is likely to provide better randomness.
-
-
Method Detail
-
next
protected int next(int bits)
Returns a pseudo-random uniformly distributedintvalue of the number of bits specified by the argumentbitsas 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.
-
nextBoolean
public boolean nextBoolean()
Returns a pseudo-random uniformly distributedboolean.
-
nextBytes
public void nextBytes(byte[] buf)
Fillsbufwith random bytes.
-
nextDouble
public double nextDouble()
Returns a pseudo-random uniformly distributeddoublein the half-open range [0.0, 1.0).
-
nextFloat
public float nextFloat()
Returns a pseudo-random uniformly distributedfloatin the half-open range [0.0, 1.0).
-
nextGaussian
public double nextGaussian()
Returns a pseudo-random (approximately) normally distributeddoublewith 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.
-
nextInt
public int nextInt()
Returns a pseudo-random uniformly distributedint.
-
nextInt
public int nextInt(int n)
Returns a pseudo-random uniformly distributedintin the half-open range [0, n).
-
nextLong
public long nextLong()
Returns a pseudo-random uniformly distributedlong.
-
setSeed
public 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.
-
-