π² Java Program to Generate Random Number β With Examples & Methods
π§² Introduction
Random numbers play a key role in gaming, simulations, security, and testing applications. In Java, generating a random number is easy using built-in classes like Math
, Random
, and ThreadLocalRandom
.
By the end of this article, youβll learn how to:
- β Generate random integers and decimals
- β
Use
Math.random()
,Random
, andThreadLocalRandom
- β Set ranges and apply best practices
π’ 1. Using Math.random()
β Simple & Quick
public class RandomMath {
public static void main(String[] args) {
double randomValue = Math.random(); // 0.0 <= value < 1.0
System.out.println("Random number (0β1): " + randomValue);
int randomInt = (int) (Math.random() * 100); // 0 to 99
System.out.println("Random integer (0β99): " + randomInt);
}
}
β Explanation:
Math.random()
generates a double from 0.0 to less than 1.0.- Multiply and cast to get a specific integer range.
π‘ Best for quick, lightweight randomness.
π² 2. Using java.util.Random
β More Control
import java.util.Random;
public class RandomWithClass {
public static void main(String[] args) {
Random rand = new Random();
int randInt = rand.nextInt(100); // 0β99
double randDouble = rand.nextDouble(); // 0.0β1.0
System.out.println("Random integer (0β99): " + randInt);
System.out.println("Random double (0β1): " + randDouble);
}
}
β Explanation:
nextInt(bound)
gives you a range [0, bound).nextDouble()
gives 0.0 β€ value < 1.0.
π‘ Use this when you need repeatable or seedable randomness.
β‘ 3. Using ThreadLocalRandom
β Recommended for Multi-Threading
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomExample {
public static void main(String[] args) {
int randomInt = ThreadLocalRandom.current().nextInt(50, 101); // 50 to 100
double randomDouble = ThreadLocalRandom.current().nextDouble(1.5, 3.5);
System.out.println("Random int (50β100): " + randomInt);
System.out.println("Random double (1.5β3.5): " + randomDouble);
}
}
β Explanation:
- Supports inclusive lower and exclusive upper bounds.
- Safe and efficient in multi-threaded applications.
π Comparison Table β Random Generation in Java
Method | Type | Thread Safe? | Range Control | Use Case |
---|---|---|---|---|
Math.random() | double | β Yes | β Manual | Quick one-liners |
new Random() | int , double | β No | β Yes | General-purpose, seedable |
ThreadLocalRandom | int , double | β Yes | β Yes | High-performance, multi-thread |
π§ͺ Generate Random Boolean or Float
Random rand = new Random();
boolean randBool = rand.nextBoolean();
float randFloat = rand.nextFloat(); // 0.0 β 1.0
System.out.println("Random boolean: " + randBool);
System.out.println("Random float: " + randFloat);
β Works with all primitive types.
π Summary
Youβve now mastered:
- β
Generating random numbers using
Math.random()
,Random
, andThreadLocalRandom
- β Controlling ranges and types (int, double, boolean)
- β Writing clean and safe randomization code
Random numbers are a core tool for games, security, simulations, and even AI.
β FAQ β Java Random Number Generation
βHow to generate a random number between 1 and 10?
int randNum = ThreadLocalRandom.current().nextInt(1, 11);
β Generates values from 1 to 10 (upper bound is exclusive).
βCan I generate the same random sequence every time?
Yes, by setting a seed:
Random seededRand = new Random(42); // fixed seed
System.out.println(seededRand.nextInt(100));
βWhich method is best for performance?
ThreadLocalRandom
is best for multi-threaded or high-frequency random generation.
Share Now :