Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / security

Difference between Random, SecureRandom and ThreadLocalRandom

5.00/5 (2 votes)
12 Feb 2023CPOL2 min read 7K  
Examining the differences between java.util.Random, java.security.SecureRandom, and java.util.concurrent.ThreadLocalRandom to generate random numbers
This post examines differences between java.util.Random, java.security.SecureRandom and java.util.concurrent.ThreadLocalRandom and tells when to use which one of the classes to use to generate random numbers.

In Java, using java.util.Random, java.security.SecureRandom, and java.util.concurrent.ThreadLocalRandom, we can generate random numbers.

In this tutorial, let's examine the differences between these classes and when to use each.

1. java.util.Random

The Random class is used to generate a stream of pseudo-random numbers. It is simpler to use and suitable for many applications.

Here is an example of how to generate a random number using java.util.Random class.

Java
import java.util.Random;

public class RandomTest { 
    public static void main(String[] args) { 
        Random random = new Random(); 

        // Generate a random number between 0 and 10 (inclusive)
        int randomNumber = random.nextInt(11); 
        System.out.println("Random number: " + randomNumber); 
     }
}

But there are two downsides to it:

  1. Not cryptographically secure
  2. Not performant efficient when used in multi-threaded environments

SecureRandom and ThreadLocalRandom are the two classes that can be used to overcome the above problems.

2. java.security.SecureRandom

SecureRandom is a subclass of java.util.Random and is the preferred choice to generate random numbers in security-sensitive applications. It uses a cryptographically strong pseudorandom generator to generate random numbers.

This CSRNG (Cryptographically Strong Random Number Generator) complies with the security requirements of Cryptography modules and all the random numbers generated are cryptographically strong and unpredictable.

Here is an example of how to generate random numbers using SecureRandom class.

Java
package random; 
import java.security.SecureRandom; 

public class SecureRandomTest { 
    public static void main(String[] args) {
        SecureRandom secureRandom = new SecureRandom(); 
        int randomInt = secureRandom.nextInt(); 
        System.out.println("Random number: " + randomInt); 
    }
}

3. java.util.concurrent.ThreadLocalRandom

java.util.concurrent.ThreadLocalRandom is a subclass of java.util.Random that is optimized for multi-threaded environments. It uses thread-local storage to generate random numbers so that each thread can generate its own sequence of random numbers without interference from other threads.

You can use the current() method to obtain the current instance of the ThreadLocalRandom class and then use the various methods provided by the class to generate random numbers. Here are a few examples:

Java
package random; 
import java.util.concurrent.ThreadLocalRandom; 

public class ThreadLocalRandomTest { 
    public static void main(String[] args) { 
        // Generate a random integer between 0 and 100 
        int randomInt = ThreadLocalRandom.current().nextInt(100); 
        System.out.println("Random integer: " + randomInt); 

        // Generate a random double between 0 and 1 
        double randomDouble = ThreadLocalRandom.current().nextDouble();
        System.out.println("Random double: " + randomDouble); 

        // Generate a random long between 0 and 1000000
        long randomLong = ThreadLocalRandom.current().nextLong(1000000); 
        System.out.println("Random long: " + randomLong); 
    }
}

4. Conclusion

In general, if you need to generate random numbers for cryptography, you should use java.security.SecureRandom. If you need to generate random numbers in a multi-threaded environment with no thread congestion and no performance penalty, you should use java.util.concurrent.ThreadLocalRandom. Otherwise, if you just need to generate random numbers for general-purpose applications, you can use java.util.Random.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)