Trusted answers to developer questions

How to generate random numbers in Java

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

A random number is a value chosen without any predictable pattern. Random numbers within a specific range of type integer, float, double, long, and boolean can be generated in Java. In this Answer, we will learn four methods to generate random numbers in Java.

  • Using the java.util.random class
  • Using the Math.random() method
  • Using the ThreadLocalRandom class
  • 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 class java.util.Random
  2. Make the instance of the class Random, i.e., Random rand = new Random()
  3. Invoke one of the following methods of rand object:
    • nextInt(upperbound) generates random numbers in the range 0 to upperbound-1.
    • nextFloat() generates a float between 0.0 and 1.0.
    • nextDouble() generates a double between 0.0 and 1.0.
import java.util.Random;
class GenerateRandom {
public static void main( String args[] ) {
// Instance of random class
Random rand = new Random();
// Setting the upper bound to generate the
// random numbers in specific range
int upperbound = 25;
// Generating random numbers from 0 - 24
// using nextInt()
int int_random = rand.nextInt(upperbound);
// Generating random numbers using nextDouble
// in 0.0 and 1.0 range
double double_random = rand.nextDouble();
// Generating random numbers using nextFloat
// in 0.0 and 1.0 range
float float_random = rand.nextFloat();
// Printing the generated random numbers
System.out.println("Random integer value from 0 to" + (upperbound-1) + " : " + int_random);
System.out.println("Random float value between 0.0 and 1.0 : " + float_random);
System.out.println("Random double value between 0.0 and 1.0 : " + double_random);
}
}

This class also allows us to generate long and boolean type random numbers using the nextLong() and nextBoolean() methods, respectively. You can see examples of these methods in this Answer.

Method 2: Using the Math.random() method

The Math.random() function in Java generates a double-type random number between 0 (including) and 1 (excluding).

class GenerateRandom {
public static void main( String args[] ) {
// Generate random number
System.out.println("Random number: " + Math.random());
}
}

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 you need an integer or float random value.

class GenerateRandom {
public static void main( String args[] ) {
int min = 50; // Minimum value of range
int max = 100; // Maximum value of range
// Print the min and max
System.out.println("Random value in int from "+ min + " to " + max + ":");
// Generate random int value from min to max
int random_int = (int)Math.floor(Math.random() * (max - min + 1) + min);
// Printing the generated random numbers
System.out.println(random_int);
}
}

Method 3: Using the ThreadLocalRandom class

To generate random numbers using the class ThreadLocalRandom, follow the steps below:

  1. Import the class java.util.concurrent.ThreadLocalRandom
  2. Invoke one of the following methods:
    • To generate random number of type int ThreadLocalRandom.current().nextInt()
    • To generate random number of type double ThreadLocalRandom.current().nextDouble()
    • To generate random number of type boolean ThreadLocalRandom.current().nextBoolean()
import java.util.concurrent.ThreadLocalRandom;
class GenerateRandom {
public static void main( String args[] ) {
// Generate random integers
int int_random = ThreadLocalRandom.current().nextInt();
// Print random integers
System.out.println("Random Integers: " + int_random);
// Generate random doubles
double double_rand = ThreadLocalRandom.current().nextDouble();
// Print random doubles
System.out.println("Random Doubles: " + double_rand);
// Generate random booleans
boolean boolean_rand = ThreadLocalRandom.current().nextBoolean();
// Print random booleans
System.out.println("Random Booleans: " + boolean_rand);
}
}

Method 4: Using the SecureRandom class

Random class has a higher chance of repeating numbers during random number generation. Whereas, SecureRandom class allows us to generate cryptographically strong random numbers using the following steps:

  1. Import the SecureRandom using java.security.SecureRandom.
  2. Make the instance of SecureRandom class using new SecureRandom().
  3. Use following methods to generate the random numbers:
    • nextInt(upperbound) generates random numbers in the range 0 to upperbound-1.
    • nextFloat() generates a float between 0.0 and 1.0.
    • nextDouble() generates a double between 0.0 and 1.0.
import java.security.SecureRandom;
class GenerateRandom {
public static void main( String args[] ) {
// Instance of SecureRandom class
SecureRandom rand = new SecureRandom();
// Setting the upper bound to generate
// the random numbers between the specific range
int upperbound = 1000;
// Generating random numbers from 0 - 999
// using nextInt()
int int_random1 = rand.nextInt(upperbound);
int int_random2 = rand.nextInt(upperbound);
// Generating random numbers using nextDouble
// in 0.0 and 1.0 range
double double_random = rand.nextDouble();
// Generating random numbers using nextFloat
// in 0.0 and 1.0 range
float float_random = rand.nextFloat();
// Printing the generated random numbers
System.out.println("Random integer value from 0 to " + (upperbound - 1) + " : " + int_random1);
System.out.println("Random integer value from 0 to " + (upperbound - 1) + " : " + int_random2);
System.out.println("Random float value between 0.0 and 1.0 : " + float_random);
System.out.println("Random double value between 0.0 and 1.0 : " + double_random);
}
}

If you're interested in exploring an alternative method for generating random numbers in Java, check out this Answer, where we discuss Random.ints().

RELATED TAGS

random number in java
java random
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?