Random number generator in java

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.

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:

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:

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *