Random
An instance of this class is used to generate a stream of pseudorandom numbers. The numbers generated by the Random
class are not truly random; they are pseudorandom, meaning they are determined by a deterministic algorithm based on a seed value.
Instances of the Random
class are not thread-safe. For concurrent use, ThreadLocalRandom
or SecureRandom
is recommended.
Methods
Random()
: Creates a new random number generator using a single long seed value, initialized to a value that is likely to be distinct.
Random(long seed)
: Creates a new random number generator using the specified seed value.
nextInt()
: Returns the next pseudorandom, uniformly distributed int
value from this random number generator’s sequence. (each possible integer within the specified range has an equal probability of being selected.) Range is from Integer.MIN_VALUE
to Integer.MAX_VALUE
nextInt(int bound)
: Returns a pseudorandom, uniformly distributed int
value between 0 (inclusive) and the specified value (exclusive).
nextLong()
: Returns the next pseudorandom, uniformly distributed long
value from this random number generator’s sequence.
nextFloat()
: Returns the next pseudorandom, uniformly distributed float
value between 0.0 and 1.0 from this random number generator’s sequence.
nextDouble()
: Returns the next pseudorandom, uniformly distributed double
value between 0.0 and 1.0 from this random number generator’s sequence.
nextBoolean()
: Returns the next pseudorandom, uniformly distributed boolean
value from this random number generator’s sequence.
nextBytes(byte[] bytes)
: Generates random bytes and places them into a user-supplied byte array.
import java.util.Random;
public class RandomExample {
public static void main(String[] args) {
// Create a new instance of Random
Random random = new Random();
// Generate a random integer
int randomInt = random.nextInt();
System.out.println("Random Integer: " + randomInt);
// Generate a random integer between 0 (inclusive) and 100 (exclusive)
int randomIntBound = random.nextInt(100);
System.out.println("Random Integer (0-99): " + randomIntBound);
// Generate a random long
long randomLong = random.nextLong();
System.out.println("Random Long: " + randomLong);
// Generate a random float between 0.0 and 1.0
float randomFloat = random.nextFloat();
System.out.println("Random Float: " + randomFloat);
// Generate a random double between 0.0 and 1.0
double randomDouble = random.nextDouble();
System.out.println("Random Double: " + randomDouble);
// Generate a random boolean
boolean randomBoolean = random.nextBoolean();
System.out.println("Random Boolean: " + randomBoolean);
// Generate random bytes
byte[] byteArray = new byte[10];
random.nextBytes(byteArray);
System.out.print("Random Bytes: ");
for (byte b : byteArray) {
System.out.print(b + " ");
}
System.out.println();
// Create a seeded random number generator
Random seededRandom = new Random(12345L);
// Generate a random integer with the seeded random number generator
int seededRandomInt = seededRandom.nextInt();
System.out.println("Seeded Random Integer: " + seededRandomInt);
}
}
Use of Seed
import java.util.Random;
public class SeedExample {
public static void main(String[] args) {
// Seeded Random Number Generators
Random random1 = new Random(12345L);
Random random2 = new Random(12345L);
// Generating a sequence of numbers from both generators
for (int i = 0; i < 5; i++) {
System.out.println("random1: " + random1.nextInt());
System.out.println("random2: " + random2.nextInt());
}
// Changing the seed
Random random3 = new Random(67890L);
for (int i = 0; i < 5; i++) {
System.out.println("random3: " + random3.nextInt());
}
}
}
Pseudorandom Numbers: Numbers that are generated using deterministic processes (algorithms) but appear to be random. These numbers are not truly random because their generation depends on initial values known as seeds.
Deterministic Nature: Given the same initial conditions (seed), a pseudorandom number generator (PRNG) will produce the same sequence of numbers. This reproducibility is crucial for debugging and testing.
Seed: An initial value used by a PRNG to start the number generation process. The seed determines the starting state of the algorithm. The sequence of pseudorandom numbers is entirely determined by the seed value. Changing the seed changes the sequence of generated numbers.
Default Seed: If no seed is provided, most PRNGs will use the current time (in milliseconds since the epoch).