How to generate random numbers in Java

Key takeaways:

  • java.util.Random generates random values in various formats.

  • Math.random() produces a random double in the interval from 0 (inclusive) to 1 (exclusive). Ideal for straightforward random number generation and scaling to other ranges.

  • ThreadLocalRandom provides thread-safe random number generation for integers, doubles, and booleans. It is best suited for multithreaded applications requiring efficient randomization.

  • random.ints() provides an infinite stream of random integers within a specified range, allowing flexible and powerful random number generation in Java streams.

  • SecureRandom produces cryptographically secure random numbers. It is essential for applications needing high-level security, such as encryption and secure token generation.

  • Each method caters to different needs, ranging from basic randomization to cryptographic security, allowing developers to select the appropriate tool for their specific use cases.

Random number generation

A random number is an arbitrary number that does not follow any specific pattern. John von Neumann introduced the first method in 1946 for pseudo-random numberPseudo-random numbers are developed by a mathematical formula or an algorithm in which a series of numbers is produced from an initial seed value. The algorithms are deterministic, so if the ‘seed’ value is used again, the sequence of random numbers will be similar each time. generation known as middle-square method.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Random number generation can help us in gaming, such as controlling dice rolls OTPOne-time password codes, and in cryptography for generating secure keys and encryption. Java provides several built-in ways to create random numbers, and Java random numbers can be of various types, such as integers and doubles of the specified range. In this Answer, we’ll explore five different methods for Java random number generators.

  • Using the java.util.Random class
  • Using the Math.random() method
  • Using the ThreadLocalRandom class
  • Using the Random.ints() method
  • Using the SecureRandom class

Method 1: Using the java.util.Random class

To use the Random class to generate random numbers, follow the steps below:

  1. Import the java.util.Random class.
  2. Make the instance of the Random class, i.e., Random rand = new Random().
  3. Ue some of the following Random class methods on rand object:
    • nextInt(upperbound): This generates random integers in the range 0 to upperbound-1.
    • nextFloat(): This generates a float between 0.0 and 1.0.
    • nextDouble(): This generates a double between 0.0 and 1.0.
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

This class also allows us to generate long and boolean-type random numbers using the nextLong() and nextBoolean() methods, respectively.

Method 2: Using the Math.random() method

The Java Math.random() method generates a random double-precision floating-point number between 0. 0 (inclusive) and 1.0 (exclusive).

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

For generating random numbers within a range using the Math.random(), follow the steps below:

  1. Declare the minimum value of the range.
  2. Declare the maximum value of the range.
  3. Use the formula Math.floor(Math.random() * (max - min + 1) + min) to generate values, with the min and the max value inclusive.

Note: This method can only be used if we need an integer or float of a specified random number range.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Method 3: Using the ThreadLocalRandom class

ThreadLocalRandom class in Java provides the mechanism to generate pseudorandom numbers in a safe thread model by providing each thread with its random number generator so that there is no conflict between the threads in the multithreaded environment. Additionally, this class uses an internally generated seed value that cannot be modified.

To use the ThreadLocalRandom class, follow the steps below:

  1. Import the java.util.concurrent.ThreadLocalRandom class.
  2. Use some of the following ThreadLocalRandom class methods:
    • To generate a random integer, use ThreadLocalRandom.current().nextInt().
    • To generate a random double-precision floating-point number, use ThreadLocalRandom.current().nextDouble().
    • To generate a random boolean value, use ThreadLocalRandom.current().nextBoolean().
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Method 4: Using the random.ints() method

The ints is an instance method of the Random class that is used to generate a stream of random integers in Java 8. There are four different variants of this method, namely:

  • ints(long streamSize): Generates a stream of pseudorandom random integers with the specified number of elements.

  • ints(): Produces an infinite stream of random integers without any specific bounds or limits.

  • ints(long streamSize, int randomNumberOrigin, int randomNumberBound): Generates a stream of pseudorandom integers with the specified number of elements. The value of each random integer is between a lower bound randomNumberOrigin (included) and an upper bound randomNumberBound (excluded).

  • ints(int randomNumberOrigin, int randomNumberBound): Generates an infinite stream of pseudorandom integers that adhere to the provided lower bound (included) and upper bound (excluded).

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Method 5: Using the SecureRandom class

The Random class has a higher chance of repeating numbers during random number generation. But, the SecureRandom class mitigates the issue by allowing us to generate random numbers that are cryptographically strong using the following steps:

  1. Import SecureRandom using java.security.SecureRandom.
  2. Make the instance of the SecureRandom class using new SecureRandom().
  3. Use some of the following SecureRandom class methods on rand object:
    • nextInt(): This generates the next pseudorandom int value from the sequence of numbers maintained by the SecureRandom object.
    • nextInt(upperbound): This generates random numbers in the range 0 to upperbound-1.
    • nextFloat(): This generates a float between 0.0 and 1.0.
    • nextDouble(): This generates a double between 0.0 and 1.0.

Note: An article in the Nature magazine explains that for a pseudorandom number generator to be cryptographically secure, it must display two characteristics:

  • Pass statistical randomness tests: The numbers they generate need to look random and unpredictable.

  • Stay secure under attack: If a part of the initial or running state of the program becomes available due to an attack, a malicious actor should not be able to guess the other values based on the available information.

If a random number generator meets the above conditions, it would generate random sequences that have a high entropy, zero correlation, and no repetition of subsequences.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Selecting a suitable random number generation method

Each method meets different needs, from general randomization to high-security applications, allowing developers to choose the most suitable method based on their requirements. Choosing the right method for generating random numbers is key to ensure robustness in your applications, as Donald Knuth once said:

Note: Random numbers should not be generated with a method chosen at random.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved