In Java, random numbers are generated using pseudo-random algorithms, meaning they follow a deterministic sequence but appear random. The two primary approaches are:
java.util.Random Class
This is the most commonly used method for generating random numbers in Java. It provides various methods to generate different types of random values.
Random random = new Random();creates a new random number generator using the current time as the seed.nextInt(int bound)returns a random integer from 0 (inclusive) to bound (exclusive).
For example:random.nextInt(100)generates a number between 0 and 99.nextInt()returns a random integer in the full range ofint(including negative values).nextDouble()returns a double between 0.0 (inclusive) and 1.0 (exclusive).nextBoolean()returnstrueorfalsewith equal probability.
To generate a random number between 1 and 100, use:
Random random = new Random();
int number = random.nextInt(100) + 1;Math.random() Method
This is a static method that returns a double value between 0.0 (inclusive) and 1.0 (exclusive).
Math.random()is simpler to use but requires manual scaling and casting for integers.To generate a number between 1 and 100:
int number = (int)(Math.random() * 100) + 1;
ThreadLocalRandom (Java 7+)
For multi-threaded applications, ThreadLocalRandom is preferred as it avoids contention and is more efficient.
Use:
ThreadLocalRandom.current().nextInt(1, 101);
This generates a number from 1 (inclusive) to 101 (exclusive), i.e., 1 to 100.
✅ Best Practice: Use
java.util.Randomfor general use,ThreadLocalRandomin concurrent environments, andMath.random()only when simplicity is key and you're okay with casting and scaling.
Videos
I am trying to simulate a coin flip, to do this I generate a random number either 0 or 1. 0 for heads, 1 for tails. I tested this by flipping a coin 1000 times, but 66% of the times it is heads.
int coin_flip() {
Random rand = new Random();
int flip = rand.nextInt(2);
return flip ;
}
The first solution is to use the java.util.Random class:
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
Another solution is using Math.random():
double random = Math.random() * 49 + 1;
or
int random = (int)(Math.random() * 50 + 1);
int max = 50;
int min = 1;
1. Using Math.random()
double random = Math.random() * 49 + 1;
or
int random = (int )(Math.random() * 50 + 1);
This will give you value from 1 to 50 in case of int or 1.0 (inclusive) to 50.0 (exclusive) in case of double
Why?
random() method returns a random number between 0.0 and 0.9..., you multiply it by 50, so upper limit becomes 0.0 to 49.999... when you add 1, it becomes 1.0 to 50.999..., now when you truncate to int, you get 1 to 50. (thanks to @rup in comments). leepoint's awesome write-up on both the approaches.
2. Using Random class in Java.
Random rand = new Random();
int value = rand.nextInt(50);
This will give value from 0 to 49.
For 1 to 50: rand.nextInt((max - min) + 1) + min;
Source of some Java Random awesomeness.