Java Utility Library

Java.util.Random Class



Java Random Class

Java.util package provides a Random class. An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Class declaration

The declaration of java.util.Random class is:

public class Random
  extends Object
    implements Serializable

Class Constructors

S.NConstructors & Description
1. Random()
Creates a new random number generator.
2. Random(long seed)
Creates a new random number generator using a single long seed.

java.util.Random Methods

The java.util.Random class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. DoubleStream doubles()
Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
2. DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
3. DoubleStream doubles(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
4. DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
5. IntStream ints()
Returns an effectively unlimited stream of pseudorandom int values.
6. IntStream ints(int randomNumberOrigin, int randomNumberBound)
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
7. IntStream ints(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom int values.
8. IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
9. LongStream longs()
Returns an effectively unlimited stream of pseudorandom long values.
10. LongStream longs(long randomNumberOrigin, long randomNumberBound)
Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
11. LongStream longs(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom long values.
12. LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
13. protected int next(int bits)
Generates the next pseudorandom number.
14. boolean nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
15. void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.
16. double nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
17. float nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
18. double nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
19. int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
20. int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
21. long nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
22. void setSeed(long seed)
Sets the seed of this random number generator using a single long seed.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object

5