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.
Random
is used for generating random numbers for applications like simulations, games, random sampling, and cryptography (though for cryptography,SecureRandom
is 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
Random
class is not suitable for cryptographic purposes due to its predictability; for secure random numbers, Java provides theSecureRandom
class.
Features
Deterministic Random Number Generation: The
Random
class 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
Random
class 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,
Random
is efficient for general use cases like simulations and games.Thread-Local Random: Since Java 1.7, the
ThreadLocalRandom
class 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 randomdouble
from 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
Was this helpful?