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.

Related Posts

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-616fcf7637747468179707/] When you run above program, […]

  • 30 March

    java random number between 1 and 10

    Table of ContentsUsing random.nextInt() to generate random number between 1 and 10 Using Math.random() to generate random number between 1 and 10 Using ThreadLocalRandom.current.nextInt() to generate random number between 1 and 10 We have already seen random number generator in java.In this post, we will address specific query on how to generate random number between […]

  • 13 December

    Java Random nextDouble

    Table of ContentsSyntaxReturnExampleGenerate double in the range In this tutorial, we will see Java Random nextDouble method.It is used to generate random double. It returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence. Syntax [crayon-616fcf7638f28068023454/] Here random is object of the java.util.Random class. Return returns random […]

  • 13 December

    Java Random nextInt

    Table of ContentsnextInt()SyntaxReturnExamplenextInt(int bound)SyntaxReturnExample In this tutorial, we will see Java Random nextInt method.It is used to generate random integer. There are two overloaded versions for Random nextInt method. nextInt() Syntax [crayon-616fcf7639a18737695829/] Here random is object of the java.util.Random class. Return returns random integer. Example Let’s see a very simple example: [crayon-616fcf7639a20981238567/] Output: Random Integer: […]

  • 13 December

    Java – generate random String

    Table of ContentsUsing simple java code with RandomUsing Apache Common langCreate AlphaNumericStringCreate random Alphabetic String In this tutorial, we will see how to generate random String in java. There are many ways to generate random String.Let’s explore some of ways to generate random String. Using simple java code with Random You can use SecureRandom class […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.