Random numbers can be generated in Java using methods listed below:
java.util.Random
classMath.random()
methodjava.util.concurrent.ThreadLocalRandom
classjava.security.SecureRandom
class
Using java.util.Random class
This class can be used to create random numbers. Instances of java.util.Random
are threadsafe. It provides several methods to generate random integer, long, double etc. This class provides two constructors:
Random() | Creates a new random number generator. |
Random(long seed) | Creates a new random number generator using a single long seed. |
We can set seed value using the following method:
void setSeed(long seed)
– Sets the seed of this random number generator using a single long seed.
This class provides following two important methods to generate random values:
public int nextInt() | Returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence. |
public int nextInt(int n) | Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence. |
import java.util.Random; public class RandomNumberExample { public static void main(String[] args) { Random r1 = new Random(); System.out.println("Printing 5 random int numbers:"); for (int i = 0; i < 5; i++) { System.out.println(r1.nextInt()); } Random r2 = new Random(); System.out.println("Printing 5 random int numbers between 0 and 99 :"); for (int i = 0; i < 5; i++) { System.out.println(r2.nextInt(100)); } } }
Output
Printing 5 random int numbers:
1170801530
-1301505198
115358822
691057092
-751681691
Printing 5 random int numbers between 0 and 99 :
36
79
75
88
11
java.util.Random
provides methods nextFloat(), nextLong() and nextBoolean() to return float, long and boolean random values.
Generate random float
Random random = new Random(); float f = random.nextFloat();
Generate random long
Random random = new Random(); long l = random.nextLong();
Generate random boolean
Random random = new Random(); boolean flag = random.nextBoolean();
Using Math.random() method
Following is the signature of the method:
public static double random() | Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. |
public class RandomNumberExample { public static void main(String[] args) { System.out.println("Generating random values between greater than or equal to 0.0 and less than 1.0"); for (int i = 0; i < 5; i++) { double randomNumber = Math.random(); System.out.println(randomNumber); } } }
Output
Generating random values between greater than or equal to 0.0 and less than 1.0
0.3443886698718507
0.317880505394767
0.5903146332046344
0.31153769054091385
0.9793996812023973
Using java.util.concurrent.ThreadLocalRandom Class
This class is a random number generator isolated to the current thread. A ThreadLocalRandom
is initialized with an internally generated seed that may not otherwise be modified.
This class provides following important method to generate random int value.
int nextInt(int least, int bound)
– Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
This class has other methods to get double and long values.
- double nextDouble(double n)
- double nextDouble(double least, double bound)
- long nextLong(long n)
- long nextLong(long least, long bound)
import java.util.concurrent.ThreadLocalRandom; public class RandomNumberExample { public static void main(String[] args) { ThreadLocalRandom current = ThreadLocalRandom.current(); // Generate numbers between 0 and 100 int firstRandomNumber = current.nextInt(101); int secondRandomNumber = current.nextInt(101); System.out.println("First random number: " + firstRandomNumber); System.out.println("Second random number: " + secondRandomNumber); // Generate double random values (values are between 0 (inclusive) and 1.0 // (exclusive)) double firstRandomDouble = current.nextDouble(); double secondRandomDouble = current.nextDouble(); System.out.println("First double random number: " + firstRandomDouble); System.out.println("Second double random number: " + secondRandomDouble); } }
Output
First random number: 36
Second random number: 79
First double random number: 0.2421478685304269
Second double random number: 0.03822229353077511
Using java.security.SecureRandom
This class provides a cryptographically strong random number generator.
import java.security.SecureRandom; public class RandomNumberExample { public static void main(String[] args) { SecureRandom random = new SecureRandom(); // Generate random int numbers between 0 and 100 int firstRandomNumber = random.nextInt(101); int secondRandomNumber = random.nextInt(101); System.out.println("First int: " + firstRandomNumber); System.out.println("Second int: " + secondRandomNumber); // Generate double random numbers (values are between 0 (inclusive) and 1.0 // (exclusive)) double firstRandomDouble = random.nextDouble(); double secondRandomDouble = random.nextDouble(); System.out.println("First double: " + firstRandomDouble); System.out.println("Second double: " + secondRandomDouble); } }
Output
First int: 91
Second int: 76
First double: 0.6015772017919023
Second double: 0.8063450403102967
Random numbers within a given range
For generating random numbers between a given a range, you need to specify the range. A standard expression to achieve this is:
(Math.random() * ((max - min) + 1)) + min