Random
About
The Random class in Java is part of the java.util package and is used to generate pseudo-random numbers. It provides methods for generating different types of random numbers, including integers, doubles, booleans, and long values. The random numbers produced by the Random class are based on a seed value, which influences the sequence of generated values.
Randomis used for generating random numbers for applications like simulations, games, random sampling, and cryptography (though for cryptography,SecureRandomis recommended).The class uses an internal seed value to produce a sequence of random numbers. If the same seed is used, the sequence of random numbers will be the same, which can be useful for repeatable tests or simulations.
The
Randomclass is not suitable for cryptographic purposes due to its predictability; for secure random numbers, Java provides theSecureRandomclass.
The seed is an initial value used by the random number generator (RNG) to start its internal computation of random numbers. The seed ensures that the random number generator produces a deterministic sequence of numbers, which is particularly useful for reproducibility.
How Seed Works?
When a seed is set, it initializes the internal state of the RNG. The RNG then uses this state in its algorithm (typically a pseudo-random number generation algorithm like a linear congruential generator) to produce the next number in the sequence.
If the same seed is provided, the initial state will be the same, and the sequence of generated numbers will also be identical.
Features
Deterministic Random Number Generation: The
Randomclass uses a deterministic algorithm, meaning it generates the same sequence of random numbers for the same seed.Support for Multiple Data Types: It can generate random values of various types, including
int,long,boolean,double, and arrays of bytes.Seeding: The
Randomclass allows the seeding of the random number generator to produce a repeatable sequence of random numbers. If no seed is provided, the system clock is used as the default seed.Performance: While not suitable for cryptographic purposes,
Randomis efficient for general use cases like simulations and games.Thread-Local Random: Since Java 1.7, the
ThreadLocalRandomclass was introduced to provide random number generation that avoids contention among multiple threads. It's preferred when generating random numbers in concurrent applications.
Declaration
To use the Random class, we must import it from the java.util package:
Common Methods in Random Class
Random ClassGeneration of Random Numbers:
nextInt(): Returns a random integer.nextInt(bound): Returns a random integer between 0 (inclusive) and the specified bound (exclusive).nextLong(): Returns a random long value.nextFloat(): Returns a random float between 0.0 and 1.0.nextDouble(): Returns a random double between 0.0 and 1.0.nextBoolean(): Returns a random boolean value.nextGaussian(): Returns a randomdoublefrom a Gaussian (normal) distribution with mean 0.0 and standard deviation 1.0.nextBytes(byte[] bytes): Fills the provided byte array with random bytes.
Seeding Methods:
setSeed(long seed): Sets the seed for the random number generator.Random(long seed): Constructor that initializes the random number generator with a specific seed.
State Methods:
nextInt(int bound): Returns a random integer between 0 (inclusive) and the specified bound (exclusive).nextLong(): Generates a random long number.
Usage
Basic Usage of Random Class
Random ClassUsing Random with a Seed
Random with a SeedUsing ThreadLocalRandom for Concurrent Applications
ThreadLocalRandom for Concurrent ApplicationsUsing nextGaussian() for Normal Distribution
nextGaussian() for Normal DistributionLast updated