Generating Random Numbers in Java

This article shows you about Generating Random Numbers in Java. Java provides multiple ways to generate random numbers using some pre-defined methods and classes. In this article, we will learn the following ways:

  1. Using Random class
  2. Using Math.random() method

Generating Random Numbers using Random class

In Java, you can generate random numbers using the Random class from the java.util package. The Random class provides various methods to generate random values of different data types. It offers more flexibility in controlling the random number generation process compared to Math.random().

Here are some examples of generating random numbers using the Random class in Java:

Generate random numbers between 1 and 10,000.

package com.javacodepoint.miscellaneous;

import java.util.Random;

public class GenerateRandomNumber {

	public static int generateRandom() {

		// Create object of Random class
		Random random = new Random();

		// Here nextInt() generate integer in range 0 to 9999
		// so adding +1 to generated value;
		int number = random.nextInt(10000) + 1;

		return number;
	}

	public static void main(String[] args) {

		// Generating random numbers from 1 to 10,000
		System.out.println("Random number1 => " + generateRandom());
		System.out.println("Random number2 => " + generateRandom());
		System.out.println("Random number3 => " + generateRandom());

	}

}

OUTPUT:

Random number1 => 296
Random number2 => 720
Random number3 => 4998

Generate random numbers in a specific range

package com.javacodepoint.miscellaneous;

import java.util.Random;

public class GenerateRandomNumber2 {

	// Generate random in specific range
	public static long generateRandom(int min, int max) {

		if (min >= max) {
			System.out.println("Invalid argument input");
			return 0;
		}

		// Create object of Random class
		Random random = new Random();

		// Generate integer of max
		int number = random.nextInt(max) + 1;

		// Add min value to number if it is less
		if (number < min) {
			number += min;
		}
		return number;
	}

	public static void main(String[] args) {

		// Generating random numbers in specific range
		System.out.println("Random number between 10-20 => " + generateRandom(10, 20));
		System.out.println("Random number between 50-100 => " + generateRandom(50, 100));
		System.out.println("Random number between 1000-5000 => " + generateRandom(1000, 5000));

	}

}

OUTPUT:

Random number between 10-20 => 16
Random number between 50-100 => 56
Random number between 1000-5000 => 1195

Generate random numbers of specific length

package com.javacodepoint.miscellaneous;

import java.util.Random;

public class GenerateRandomNumber3 {

	// Generate random of specific length
	public static long generateRandom(int length) {

		// Create object of Random class
		Random random = new Random();

		// Create char array of given length
		char[] digits = new char[length];

		// Avoid first digit not to store value 0
		digits[0] = (char) (random.nextInt(9) + '1');

		// Storing remaining digits
		for (int i = 1; i < length; i++) {
			digits[i] = (char) (random.nextInt(10) + '0');
		}
		// Finally return the value
		return Long.parseLong(new String(digits));
	}

	public static void main(String[] args) {

		// Generating random numbers of specific length
		System.out.println("Random number of 3 digits => " + generateRandom(3));
		System.out.println("Random number of 5 digits => " + generateRandom(5));
		System.out.println("Random number of 10 digits => " + generateRandom(10));

	}

}

OUTPUT:

Random number of 3 digits => 372
Random number of 5 digits => 32325
Random number of 10 digits => 4354677830

Generate random numbers using Math.random() method

In Java, you can generate random numbers using the Math.random() method. The Math.random() method returns a double value greater than or equal to 0.0 and less than 1.0. By using this value, you can create random numbers within a specific range or generate random numbers for various purposes.

Here are some examples of generating random numbers using the Math.random() method in Java:

Generate a random double between 0.0 and 1.0:

double randomValue = Math.random();
System.out.println("Random Double: " + randomValue);

Generate a random integer within a specific range:

int min = 1;
int max = 100;
int randomInt = min + (int)(Math.random() * (max - min + 1));
System.out.println("Random Integer between " + min + " and " + max + ": " + randomInt);

Generate a random float within a specific range:

float min = 0.0f;
float max = 10.0f;
float randomFloat = min + (float)(Math.random() * (max - min));
System.out.println("Random Float between " + min + " and " + max + ": " + randomFloat);

Generate a random number within a specific range with decimal precision:

double min = 5.0;
double max = 15.0;
double randomValue = min + (Math.random() * (max - min));
System.out.println("Random Value between " + min + " and " + max + ": " + randomValue);

Remember that Math.random() returns a value between 0.0 (inclusive) and 1.0 (exclusive). To adjust the range, you can multiply by the desired range and add the minimum value accordingly. Additionally, type casting may be needed to obtain integer or float values, depending on the desired output.

See also: Generate Random String in Java | Generating OTP in Java

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *