In this tutorial, we will see about random number generators.You might have requirement where you want to generate random numbers to perform some operation.
For example:
Let’s say you are creating a game which uses dice for each player’s turn. You can put logic to generate random number whenever the player uses dice.
There are many ways to generate random numbers in java.Let’s see each with the help of example.
Table of Contents
Using Random class
You can use java.util.Random to generate random numbers in java.You can generate integers, float, double, boolean etc using Random class.
Let’s understand with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package org.arpit.java2blog; import java.util.Random; public class RandomClassGeneratorMain { public static void main(String[] args) { Random randomGenerator=new Random(); System.out.println("============================"); System.out.println("Generating 5 random integers"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(randomGenerator.nextInt()); } System.out.println("============================"); System.out.println("Generating 5 random double"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(randomGenerator.nextDouble()); } System.out.println("============================"); System.out.println("Generating 5 random floats"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(randomGenerator.nextFloat()); } System.out.println("============================"); System.out.println("Generating 5 random booleans"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(randomGenerator.nextBoolean()); } } } |
Output:
Generating 5 random integers
============================
1342618771
-1849662552
1719085329
2141641685
-819134727
============================
Generating 5 random doubles
============================
0.1825454639005325
0.5331492085899436
0.830900901839756
0.8490109501015005
0.7968080535091425
============================
Generating 5 random floats
============================
0.9831014
0.24019146
0.11383718
0.42760438
0.019532561
============================
Generating 5 random booleans
============================
false
false
true
true
false
Using ThreadLocalRandom class
You can use ThreadLocalRandom class to generate random numbers.This class got introduced in Java 7.Although java.util.Random is thread safe but multiple threads tries to access same object, there will be lot of contention and performance issue.In case of ThreadLocalRandom, each thread will generate their own random numbers and there won’t be any contention.
Let’s understand with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; import java.util.concurrent.ThreadLocalRandom; public class ThreadLocalRandomMain { public static void main(String[] args) { System.out.println("============================"); System.out.println("Generating 5 random integers"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(ThreadLocalRandom.current().nextInt()); } System.out.println("============================"); System.out.println("Generating 5 random doubles"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(ThreadLocalRandom.current().nextDouble()); } System.out.println("============================"); System.out.println("Generating 5 random floats"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(ThreadLocalRandom.current().nextFloat()); } System.out.println("============================"); System.out.println("Generating 5 random booleans"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(ThreadLocalRandom.current().nextBoolean()); } } } |
Output:
Generating 5 random integers
============================
-315342453
-1922639586
-19084346
-615337866
-1075097641
============================
Generating 5 random doubles
============================
0.9074981945011997
0.7626761438609163
0.4439078754038527
0.8663565773294881
0.8133933685024771
============================
Generating 5 random floats
============================
0.50696343
0.4109127
0.4284398
0.37340754
0.28446126
============================
Generating 5 random booleans
============================
false
true
false
false
true
Using Math.random method
You can use Math.random’s method to generate random doubles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; public class MathRandomMain { public static void main(String[] args) { System.out.println("============================"); System.out.println("Generating 5 random doubles"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(Math.random()); } } } |
Output:
Generating 5 random doubles
============================
0.3644159931296438
0.07011727069753859
0.7602271011682066
0.914594143579762
0.6506514073704143
Generate random numbers between range
If you want to generate random numbers between certain range, you can use Random and ThreadLocalRandom to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class GenerateRandomInRangeMain { public static void main(String[] args) { int minimum=10; int maximum=20; System.out.println("============================"); System.out.println("Generating 5 random integer in range of 10 to 20 using Random"); System.out.println("============================"); Random randomGenerator=new Random(); for (int i = 0; i < 5; i++) { System.out.println(randomGenerator.nextInt((maximum - minimum) + 1) + minimum); } System.out.println("============================"); System.out.println("Generating 5 random integer in range of 10 to 20 using ThreadLocalRandom"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(ThreadLocalRandom.current().nextInt(minimum,maximum+1)); } System.out.println("============================"); System.out.println("Generating 5 random integer in range of 10 to 20 using Math.random"); System.out.println("============================"); for (int i = 0; i < 5; i++) { System.out.println(minimum + (int)(Math.random() * ((maximum - minimum) + 1))); } } } |
Output:
Generating 5 random integer in range of 10 to 20 using Random
============================
11
18
14
13
15
============================
Generating 5 random integer in range of 10 to 20 using ThreadLocalRandom
============================
10
12
13
13
16
============================
Generating 5 random integer in range of 10 to 20 using Math.random
============================
14
10
16
20
15
That’s all about generating random numbers in java.