Java Program to Generate Random Numbers

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

Java Program to Generate Random Numbers using Math function

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

package RemainingSimplePrograms;

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

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

package RemainingSimplePrograms;

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

Until now, we generated a single value between a range. However, with the help of for loop, we can generate them within a range.

public class Example {	

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

Java Program to Generate Random Numbers using class

This random class has different methods to generate numbers. They are

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

import java.util.Random;

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

In this 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 Example5 {
	
	public static void main(String[] args) {
		
		Random rand = new Random();
	
		for(int i = 1; i <= 10; i++)
		{
			System.out.print(rand.nextInt(100) + 1 + "   ");
		}
	}
}
9   45   62   84   18   2   10   33   28   60 

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 Example6 {
	
	public static void main(String[] args) {
			
		System.out.println("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("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("Float Values");
		System.out.println(ThreadLocalRandom.current().nextFloat());
		System.out.println(ThreadLocalRandom.current().nextFloat());
		
		System.out.println("Long Values");
		System.out.println(ThreadLocalRandom.current().nextLong());
		System.out.println(ThreadLocalRandom.current().nextLong(40));
		System.out.println(ThreadLocalRandom.current().nextLong(400, 600));
	}
}
Integer Values
536433268
17
105
Double Values
0.35695024289926547
29.875628014935078
45.29405097588917
Float Values
0.62107533
0.9211104
Long Values
-1523132321353307594
4
542

This program uses ThreadLocalRandom and the for loop.

package RemainingSimplePrograms;

import java.util.concurrent.ThreadLocalRandom;

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