Java Program to Generate Random Numbers

Write a Java program to generate random numbers with an example. For example, in Java, we can use the Math random() method, Random class, and ThreadLocalRandom class to generate random numbers.

Java Program to Generate Random Numbers using Math random()

We use the Math.random() method in this Java example, which generates random numbers between 0 and 1 of double type. We can also typecast the output to get the necessary data type.

package RemainingSimplePrograms;

public class RandomNumbers1 {
	
	public static void main(String[] args) {
	
		System.out.println("The First Random Number  = " + Math.random());
		
		System.out.println("The Second Random Number = " + Math.random());
		
		System.out.println("The Third Random Number  = " + Math.random());
		
		System.out.println("The Fourth Random Number = " + Math.random());
	}
}
The First Random Number  = 0.20037158212760142
The Second Random Number = 0.0012068894419960952
The Third Random Number  = 0.18700555955501663
The Fourth Random Number = 0.3230234616758232

By using the Java Math.random() * (max – min + 1) + min we can generate the random numbers between min and max value.

package RemainingSimplePrograms;

public class RandomNumbers2 {
	
	public static void main(String[] args) {
		
		int min = 100, max = 300;
	
		System.out.print("The Double Random Number between " + min + " and " + max  + " = " );
		double rand1 = Math.random() * (max - min + 1) + min;
		System.out.print(rand1);
		
		System.out.print("\nThe Random Number between " + min + " and " + max  + " = " );
		int rand2 = (int)(Math.random() * (max - min + 1) + min);
		System.out.print(rand2);
	}
}
The Double Random Number between 100 and 300 = 101.43411590322778
The Random Number between 100 and 300 = 282

Until now, we generated single random between a range. However, with the help of Java for loop, we can generate N number of random numbers within a range.

package RemainingSimplePrograms;

public class RandomNumbers3 {
	
	public static void main(String[] args) {
		
		int min = 10, max = 100;
	
		System.out.println("Ten Random Numbers between " + min + " and " + max);
		for(int i = 1; i <= 10; i++)
		{
			int randNum = (int)(Math.random() * (max - min + 1) + min);
			System.out.print(randNum + "   ");
		}
		
		System.out.println("\nTwenty Random Numbers between 100 and 250");
		for(int i = 1; i <= 10; i++)
		{
			int randNum = (int)(Math.random() * (250 - 100 + 1) + 100);
			System.out.print(randNum + "   ");
		}
	}
}
Java Program to Generate Random Numbers 3

Java Program to Generate Random Numbers using Random class

The Java Random class has different methods to generate random numbers. They are

  • nextInt() – Generate random values between 0 and -1
  • nextInt(max_Value)
  • nextFloat() – Generate random values between 0.0 and 1.0
  • nextDouble() – Generate random values between 0.0 and 1.0
  • nextLong()
  • nextBoolean()
package RemainingSimplePrograms;

import java.util.Random;

public class RandomNumbers4 {
	
	public static void main(String[] args) {
		
		Random rand = new Random();
		
		System.out.println("Randomly Generated Integer Values");
		System.out.println(rand.nextInt());
		System.out.println(rand.nextInt(20));
		System.out.println(rand.nextInt(100));
			
		System.out.println("Randomly Generated Double Values");
		System.out.println(rand.nextDouble());
		System.out.println(rand.nextDouble());
		
		System.out.println("Randomly Generated Float Values");
		System.out.println(rand.nextFloat());
		System.out.println(rand.nextFloat());
		
		System.out.println("Randomly Generated Long Values");
		System.out.println(rand.nextLong());
		System.out.println(rand.nextLong());
	}
}
Randomly Generated Integer Values
1907114976
4
34
Randomly Generated Double Values
0.9425130257739933
0.35445514266974565
Randomly Generated Float Values
0.26128042
0.47151804
Randomly Generated Long Values
1983140345491940719
8269807721314480176

In this Java example, we used for loop to generate ten random numbers of Integer type of max limit value 100.

package RemainingSimplePrograms;

import java.util.Random;

public class RandomNumbers5 {
	
	public static void main(String[] args) {
		
		Random rand = new Random();
	
		System.out.println("Ten Randomly Generated Numbers are");
		for(int i = 1; i <= 10; i++)
		{
			System.out.print(rand.nextInt(100) + 1 + "   ");
		}
	}
}
Ten Randomly Generated Numbers are
9   45   62   84   18   2   10   33   28   60 

Java Program to Generate Random Numbers using ThreadLocalRandom

We can also use the ThreadLocalRandom to generate random numbers within a range. The nextInt(), nextDouble(), and nextLong() accepts the minimum and maximum limit value.

package RemainingSimplePrograms;

import java.util.concurrent.ThreadLocalRandom;

public class RandomNumbers6 {
	
	public static void main(String[] args) {
			
		System.out.println("Randomly Generated Integer Values");
		System.out.println(ThreadLocalRandom.current().nextInt());
		System.out.println(ThreadLocalRandom.current().nextInt(20));
		System.out.println(ThreadLocalRandom.current().nextInt(100, 500));
			
		System.out.println("Randomly Generated Double Values");
		System.out.println(ThreadLocalRandom.current().nextDouble());
		System.out.println(ThreadLocalRandom.current().nextDouble(50));
		System.out.println(ThreadLocalRandom.current().nextDouble(30, 50));
		
		System.out.println("Randomly Generated Float Values");
		System.out.println(ThreadLocalRandom.current().nextFloat());
		System.out.println(ThreadLocalRandom.current().nextFloat());
		
		System.out.println("Randomly Generated Long Values");
		System.out.println(ThreadLocalRandom.current().nextLong());
		System.out.println(ThreadLocalRandom.current().nextLong(40));
		System.out.println(ThreadLocalRandom.current().nextLong(400, 600));
	}
}
Randomly Generated Integer Values
536433268
17
105
Randomly Generated Double Values
0.35695024289926547
29.875628014935078
45.29405097588917
Randomly Generated Float Values
0.62107533
0.9211104
Randomly Generated Long Values
-1523132321353307594
4
542

This Java program generates multiple random numbers within a range using ThreadLocalRandom and the for loop.

package RemainingSimplePrograms;

import java.util.concurrent.ThreadLocalRandom;

public class RandomNumbers7 {
	
	public static void main(String[] args) {
		
		int min = 100, max = 300;
	
		System.out.println("Ten Randomly Generated Numbers are");
		for(int i = 1; i <= 10; i++)
		{
			System.out.print(ThreadLocalRandom.current().nextInt(100) + 1 + "   ");
		}
		
		System.out.println("\nFifteen Randomly Generated Numbers are");
		for(int i = 0; i < 15; i++)
		{
			int rdNum = ThreadLocalRandom.current().nextInt(min, max);
			System.out.print(rdNum + "   ");
		}
	}
}
Ten Randomly Generated Numbers are
10   88   47   68   41   79   45   74   1   41   
Fifteen Randomly Generated Numbers are
262   187   267   255   255   265   136   201   228   255   166   156   115   291   169