How to create random numbers in Java: There are various uses of random numbers in the fields of cybersecurity and game development.
In Java, there are several ways to generate random numbers by using different pre-defined classes. Out of which, some of the best ways we are going to discuss in this article.
These methods will help you to generate random numbers using just a single line of code.
We are also going to discuss the ways to create random numbers in Java within some specific range.
Let’s start with the first and most commonly used class for generating random numbers, which is Math.
Table of Contents
1. Math.random()
Math.random is a static method of the Math class using which you can generate random double values ranging between 0 and 1.
Let’s have a look at a simple example for generating a random number using the Math.random() method.
public class RandomNum {
public static void main(String[] args) {
for(int i=0; i<5; i++) {
System.out.println(Math.random());
}
}
}
Output:
0.950796819165464
0.40759668022523954
0.36811391033844043
0.6474429499284833
0.43272835762067774
As you can see, this method gives a random number between 0 and 1 only.
However, if you want to generate random numbers between custom ranges, then you can perform calculations on the given value as follows:
public class RandomNumWithRange {
public static void main(String[] args) {
int min = 1;
int max = 10;
int range = max-min+1;
for(int i=0; i<5; i++) {
System.out.println((Math.random() * range) + min);
}
}
}
Output:
5.912159462044229
8.968188043547539
9.326054270028663
10.79475309158316
1.555786549192543
Using the above approach, you can easily set the range for random numbers generated.
2. Random class
In the Random class, there are different methods available for generating random integer and double values.
For generating random integers, you can use the nextInt() method, and for generating random double values, you can make use of the nextDouble() method.
If you don’t provide any value in the argument of this method, then the range of the random integer will be between -2,147,483,648 and 2,147,483,647.
import java.util.Random;
public class RandomNum {
public static void main(String[] args) {
Random random = new Random();
for(int i=0; i<5; i++) {
System.out.println(random.nextInt());
}
System.out.println("*******************");
for(int i=0; i<5; i++) {
System.out.println(random.nextDouble());
}
}
}
Output:
-90495827
-1579348665
1863627714
505441052
694349633
*******************
0.7736850016227195
0.9634854104155247
0.646539153606072
0.0997800181287376
0.7996109521548543
However, if you provide the argument inside the method, then the range of the random numbers will be between 0 and the number provided in the argument.
import java.util.Random;
public class RandomNumWithRange {
public static void main(String[] args) {
Random random = new Random();
for(int i=0; i<5; i++) {
System.out.println(random.nextInt(10));
}
System.out.println("*******************");
for(int i=0; i<5; i++) {
System.out.println(random.nextDouble(10));
}
}
}
Output:
1
6
9
8
7
*******************
7.085493528522907
1.8922444600940758
7.340709954598331
7.015063210630492
0.8634702973577102
As you can see in the above code, we can set the range of the number generated from 0 to any value we want.
However, what if we want to set the lower bound of the range to something other than 0?
If you want to set the lower and upper bounds of the range, then you can perform the changes in the value generated as follows.
import java.util.Random;
public class RandomNumWithRange {
public static void main(String[] args) {
int min = 50;
int max = 100;
int range = max - min + 1;
Random random = new Random();
for(int i=0; i<5; i++) {
System.out.println(random.nextInt(range) + min);
}
System.out.println("*******************");
for(int i=0; i<5; i++) {
System.out.println(random.nextDouble(range) + min);
}
}
}
Output:
51
81
66
81
67
*******************
67.5571425805729
71.40064701372201
70.75070878132709
86.6918932719941
96.61902737375223
In the above code, we are setting the lower and upper bound manually. However, there is another method available named ints() in which you can set the lower and upper bound dynamically by providing in argument only.
3. Random.ints()
In the Random class, there is another method available for generating the random numbers, which is ints().
Using this method, you can generate a stream of random numbers between -2,147,483,648 and 2,147,483,647.
Let’s see an example.
import java.util.Random;
import java.util.stream.IntStream;
public class RandomNum {
public static void main(String[] args) {
Random random = new Random();
IntStream randNum = random.ints(5);
randNum.forEach(System.out::println);
}
}
Output:
2000687364
-2147088032
94744500
1669143814
-1598984148
You can also add more arguments inside the methods for setting the lower and upper bounds of the range for generating random numbers.
import java.util.Random;
import java.util.stream.IntStream;
public class RandomNumWithRange {
public static void main(String[] args) {
Random random = new Random();
IntStream randNum = random.ints(5, 50, 100);
randNum.forEach(System.out::println);
}
}
Output:
75
98
67
64
70
Until here, we were performing a weak random number generation technique. But there is a separate class available in Java for strong random number generation. Let’s have a look.
4. SecureRandom class
For cybersecurity and cryptography, we make use of methods of the SecureRandom class.
import java.security.SecureRandom;
public class RandomNum {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
int intRandom = secureRandom.nextInt();
double doubleRandom = secureRandom.nextDouble();
float floatRandom = secureRandom.nextFloat();
System.out.println(intRandom);
System.out.println(doubleRandom);
System.out.println(floatRandom);
}
}
Output:
1978051192
0.22030310698708844
0.70993835
The above code generates a random number between -2,147,483,648 and 2,147,483,647.
You can also set the range from 0 to any desired number as follows.
import java.security.SecureRandom;
public class RandomNumWithRange {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
int intRandom = secureRandom.nextInt(60,100);
double doubleRandom = secureRandom.nextDouble(60,100);
float floatRandom = secureRandom.nextFloat(60,100);
System.out.println(intRandom);
System.out.println(doubleRandom);
System.out.println(floatRandom);
}
}
Output:
81
67.77026219515413
70.4648
Now, let’s have a look at the last method of our list for generating random numbers.
5. ThreadLocalRandom class
If you are working in a multithreading environment, then the ThreadLocalRandom class is best for you compared to any other class for generating random numbers.
Using the ThreadLocalRandom class, you can access methods like nextInt(), nextDouble(), and nextBoolean() for generating the random Integer, Double, and Boolean values.
import java.util.concurrent.ThreadLocalRandom;
public class RandomNum {
public static void main(String[] args) {
for(int i=0; i<3; i++) {
System.out.println(ThreadLocalRandom.current().nextInt());
}
System.out.println("******************");
for(int i=0; i<3; i++) {
System.out.println(ThreadLocalRandom.current().nextDouble());
}
System.out.println("******************");
for(int i=0; i<3; i++) {
System.out.println(ThreadLocalRandom.current().nextBoolean());
}
}
}
Output:
2055150226
906158484
-1153275109
******************
0.900162590014832
0.4599816806203434
0.7458550529770052
******************
false
true
true
The range of the above-generated values is between -2,147,483,648 and 2,147,483,647.
If you want to generate the values between particular ranges only, then you can use the below code.
import java.util.concurrent.ThreadLocalRandom;
public class RandomNumWithRange {
public static void main(String[] args) {
int min = 50;
int max = 100;
int range = max - min + 1;
for(int i=0; i<5; i++) {
System.out.println(ThreadLocalRandom.current().nextInt(range) + min);
}
System.out.println("******************");
for(int i=0; i<5; i++) {
System.out.println(ThreadLocalRandom.current().nextDouble(range) + min);
}
}
}
Output:
93
65
74
50
77
******************
92.62434105251691
72.0596271029549
96.2787660870632
67.21219920425659
61.609899319501125
So, I hope you like the articles in which we have discussed some of the best methods for random number generation.
We also have an article on learning Java for competitive programming that you must take a look at. If you have any queries, let us know in the comment section below.
Well explained with nice and simple examples.