Random class in java
The Random is a built-in class in java used to generate a stream of pseudo-random numbers in java programming. The Random class is available inside the java.util package.
The Random class implements Serializable, Cloneable and Comparable interface.
🔔 The Random class is a part of java.util package.
🔔 The Random class provides several methods to generate random numbers of type integer, double, long, float etc.
🔔 The Random class is thread-safe.
🔔 Random number generation algorithm works on the seed value. If not provided, seed value is created from system nano time.
The Random class in java has the following constructors.
| S.No. | Constructor with Description |
|---|---|
| 1 |
Random()
It creates a new random number generator. |
| 2 |
Random(long seedValue)
It creates a new random number generator using a single long seedValue. |
The Random class in java has the following methods.
| S.No. | Methods with Description |
|---|---|
| 1 |
int next(int bits)
It generates the next pseudo-random number. |
| 2 |
Boolean nextBoolean()
It generates the next uniformly distributed pseudo-random boolean value. |
| 3 |
double nextDouble()
It generates the next pseudo-random double number between 0.0 and 1.0. |
| 4 |
void nextBytes(byte[] bytes)
It places the generated random bytes into an user-supplied byte array. |
| 5 |
float nextFloat()
It generates the next pseudo-random float number between 0.0 and 1.0.. |
| 6 |
int nextInt()
It generates the next pseudo-random int number. |
| 7 |
int nextInt(int n)
It generates the next pseudo-random integer value between zero and n. |
| 8 |
long nextLong()
It generates the next pseudo-random, uniformly distributed long value. |
| 9 |
double nextGaussian()
It generates the next pseudo-random Gaussian distributed double number with mean 0.0 and standard deviation 1.0. |
| 10 |
void setSeed(long seedValue)
It sets the seed of the random number generator using a single long seedValue. |
| 11 |
DoubleStream doubles()
It returns a stream of pseudo-random double values, each conforming between 0.0 and 1.0. |
| 12 |
DoubleStream doubles(double start, double end)
It retruns an unlimited stream of pseudo-random double values, each conforming to the given start and end. |
| 13 |
DoubleStream doubles(long streamSize)
It returns a stream producing the pseudo-random double values for the given streamSize number, each between 0.0 and 1.0. |
| 14 |
DoubleStream doubles(long streamSize, double start, double end)
It returns a stream producing the given streamSizenumber of pseudo-random double values, each conforming to the given start and end. |
| 15 |
IntStream ints()
It returns a stream of pseudo-random integer values. |
| 16 |
IntStream ints(int start, int end)
It retruns an unlimited stream of pseudo-random integer values, each conforming to the given start and end. |
| 17 |
IntStream ints(long streamSize)
It returns a stream producing the pseudo-random integer values for the given streamSize number. |
| 18 |
IntStream ints(long streamSize, int start, int end)
It returns a stream producing the given streamSizenumber of pseudo-random integer values, each conforming to the given start and end. |
| 19 |
LongStream longs()
It returns a stream of pseudo-random long values. |
| 20 |
LongStream longs(long start, long end)
It retruns an unlimited stream of pseudo-random long values, each conforming to the given start and end. |
| 21 |
LongStream longs(long streamSize)
It returns a stream producing the pseudo-random long values for the given streamSize number. |
| 22 |
LongStream longs(long streamSize, long start, long end)
It returns a stream producing the given streamSizenumber of pseudo-random long values, each conforming to the given start and end. |
Let's consider an example program to illustrate methods of Random class.
import java.util.Random;
public class RandomClassExample {
public static void main(String[] args) {
Random rand = new Random();
System.out.println("Integer random number - " + rand.nextInt());
System.out.println("Integer random number from 0 to 100 - " + rand.nextInt(100));
System.out.println("Boolean random value - " + rand.nextBoolean());
System.out.println("Double random number - " + rand.nextDouble());
System.out.println("Float random number - " + rand.nextFloat());
System.out.println("Long random number - " + rand.nextLong());
System.out.println("Gaussian random number - " + rand.nextGaussian());
}
}
When we execute the above code, it produce the following output.
