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.
java.util.random
classMath.random()
methodThreadLocalRandom
classSecureRandom
classjava.util.random
classTo use the Random
Class to generate random numbers, follow the steps below:
java.util.Random
Random
,
i.e.,
Random rand = new Random()
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 classRandom rand = new Random();// Setting the upper bound to generate the// random numbers in specific rangeint 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 rangedouble double_random = rand.nextDouble();// Generating random numbers using nextFloat// in 0.0 and 1.0 rangefloat float_random = rand.nextFloat();// Printing the generated random numbersSystem.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.
Math.random()
methodThe 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 numberSystem.out.println("Random number: " + Math.random());}}
For generating random numbers within a range using the Math.random()
, follow the steps below:
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 rangeint max = 100; // Maximum value of range// Print the min and maxSystem.out.println("Random value in int from "+ min + " to " + max + ":");// Generate random int value from min to maxint random_int = (int)Math.floor(Math.random() * (max - min + 1) + min);// Printing the generated random numbersSystem.out.println(random_int);}}
ThreadLocalRandom
classTo generate random numbers using the class ThreadLocalRandom
, follow the steps below:
java.util.concurrent.ThreadLocalRandom
ThreadLocalRandom.current().nextInt()
ThreadLocalRandom.current().nextDouble()
ThreadLocalRandom.current().nextBoolean()
import java.util.concurrent.ThreadLocalRandom;class GenerateRandom {public static void main( String args[] ) {// Generate random integersint int_random = ThreadLocalRandom.current().nextInt();// Print random integersSystem.out.println("Random Integers: " + int_random);// Generate random doublesdouble double_rand = ThreadLocalRandom.current().nextDouble();// Print random doublesSystem.out.println("Random Doubles: " + double_rand);// Generate random booleansboolean boolean_rand = ThreadLocalRandom.current().nextBoolean();// Print random booleansSystem.out.println("Random Booleans: " + boolean_rand);}}
SecureRandom
classRandom
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:
SecureRandom
using java.security.SecureRandom
.SecureRandom
class using new SecureRandom()
.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 classSecureRandom rand = new SecureRandom();// Setting the upper bound to generate// the random numbers between the specific rangeint 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 rangedouble double_random = rand.nextDouble();// Generating random numbers using nextFloat// in 0.0 and 1.0 rangefloat float_random = rand.nextFloat();// Printing the generated random numbersSystem.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