Random number generation – ThreadLocalRandom (concurrency /Java)

What is ThreadLocalRandom?

  • ThreadLocalRandom is used to generate random number typically for multi threading environment.
  • ThreadLocalRandom is particularly useful to generate random numbers in parallel.
  • ThreadLocalRandom offers less overhead and contention in concurrent applications as compared to java.util.Random.
  • ThreadLocalRandom is made up of ThreadLocal + Random number i.e. generate random number isolated to the current thread.
  • ThreadLocalRandom is initialized with an internally generated seed.
  • Get instance of ThreadLocalRandom of current thread using ThreadLocalRandom.current()
ThreadLocalRandom - Generate Random Number
Fig 1: ThreadLocalRandom of each thread

Methods to generate random numbers – ThreadLocalRandom

Method Name Description
int nextInt(int bound) Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).
int nextInt(int origin, int bound) Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

Examples: Generate Random Number in range (lower & upper bounds)

Example 1 : Generate random number between 0 and upper bound
 
//Get instance of ThreadLocalRandom
ThreadLocalRandom random = ThreadLocalRandom.current()
 
//Generate random number between 0 & 500
int index = -1, nRandomNumbers = 5;
while (index++ < nRandomNumbers) {
    //Generate random number within lower & upper bounds
    int randomNumber = random.nextInt(500);
//Print to console
System.out.println("\t"+randomNumber);
}
 
Example 2 : Generate random number between lower and upper bound
 
int index = -1, nRandomNumbers = 5;
while (index++ < nRandomNumbers) {
//Generate random number within lower & upper bounds
int randomNumber = random.nextInt(lowerBounds, upperBounds);
 
//Print to console
System.out.println("\t" + randomNumber);
}

Code: ThreadLocalRandom to generate random numbers in java

package org.learn.beginner;
 
import java.util.concurrent.ThreadLocalRandom;
 
public class ThreadLocalRandomNumbers {
 
    private static void generateRandomInRange(int nRandomNumbers,
                                              int lowerBounds,
                                              int upperBounds) {
        final ThreadLocalRandom random = ThreadLocalRandom.current();
 
        System.out.printf("Start generating %d random numbers between" +
                        " %d to %d\n",nRandomNumbers,lowerBounds
                                                        ,upperBounds);
 
        int index = -1;
        while (index++ < nRandomNumbers) {
            //Generate random number within lower & upper bounds
            int randomNumber = random.nextInt(lowerBounds, upperBounds);
 
            //Print to console
            System.out.println("\t" + randomNumber);
        }
        System.out.println("End generating random numbers within range");
    }
 
    private static void generateRandomNumber(int nRandomNumbers,int upperBounds) {
        final ThreadLocalRandom random = ThreadLocalRandom.current();
 
        System.out.printf("Start generating %d random numbers between" +
                        " %d to %d\n",nRandomNumbers,0, upperBounds);
 
        int index = -1;
        while (index++ < nRandomNumbers) {
            //Generate random number within lower & upper bounds
            int randomNumber = random.nextInt(upperBounds);
 
            //Print to console
            System.out.println("\t"+randomNumber);
        }
        System.out.println("End generating random numbers");
    }
 
    public static void main(String[] args) {
        int nRandomNumbers = 5;
        int upperBounds = 500;
 
        //Generate 5 random numbers
        generateRandomNumber(nRandomNumbers, upperBounds);
 
        int lowerBounds = 100;
        //Generate Random number in range (lowerBounds & upperBounds)
        generateRandomInRange(nRandomNumbers,lowerBounds,upperBounds);
    }
}

Output: generate random number in java (ThreadLocalRandom)

Start generating 5 random numbers between 0 to 500
    178
    237
    186
    39
    227
    86
End generating random numbers
Start generating 5 random numbers between 100 to 500
    157
    144
    231
    150
    471
    177
End generating random numbers within range