Java generate random number in range – Java Random int value generation classes such as ThreadLocalRandom, Random, and SplittableRandom are used to generate random integers within a specific range in Java. It allows you to generate pseudo random numbers between a certain range in Java. The classes with package names java.util.concurrent.ThreadLocalRandom, java.util.Random, Math.Random, and java.util. SplittableRandom are used to generate random integers within a given range in java.

In Java, random numbers of the types integer, float, double, long, and boolean can be generated. We’ll look at how to generate random numbers in Java, as well as examples of how to generate random integers, decimal numbers, and random numbers within a given range.



1. Using ThreadLocalRandom

In Java, the ThreadLocalRandom class is used to generate random numbers. The ThreadLocalRandom class generates a random number within the thread by isolating it within the current thread scope. The ThreadLocalRandom class will initialise and use the seed generated internally. The ThreadLocalRandom class is safe to use in concurrent java execution of the program. The ThreadLocalRandom class’s nextInt method is used to generate a random integer number in Java within the current thread.

package com.yawintutor;

import java.util.concurrent.ThreadLocalRandom;

public class RandomNumber {
	public static void main(String[] args) {
		RandomNumber r = new RandomNumber();
		System.out.println(r.random(5, 10));
	}
	
	public int random(int min, int max) {
		return ThreadLocalRandom.current().nextInt(min, max + 1);
	}
}

Output

7


2. Using Random nextInt

In Java, an instance of the Random class is used to generate a random number. A 48 bit seed is used to generate the random pseudo number, which is then modified using a linear congruential formula. Each generated random number can generate up to 32 pseudorandom bits. The pseudo random number will be generated by the Random class’s nextInt method.

package com.yawintutor;

import java.util.Random;

public class RandomNumber {
	public static void main(String[] args) {
		RandomNumber r = new RandomNumber();
		System.out.println(r.random(5, 10));
	}
	public int random(int min, int max) {
		return new Random().nextInt(max - min + 1) + min;
	}
}

Output

9


3. Using Random ints

Ints is another method in the random class (). The ints method generates an array of random numbers within a given range of values. This method generates multiple random numbers in a single API call. You can get a random number from the ints methods by accessing the index of the generated random number array. The first element in the random number array will be returned by the findFirst method.

package com.yawintutor;

import java.util.Random;

public class RandomNumber {
	public static void main(String[] args) {
		RandomNumber r = new RandomNumber();
		System.out.println(r.random(5, 10));
	}
	public int random(int min, int max) {
		return new Random().ints(min, min, max+1).findFirst().getAsInt();
	}
}

Output

8


4. Using SplittableRandom

A SplittableRandom instance is used to generate a random number within a range. Creates a new SplittableRandom instance that is expected to produce value sequences that are statistically independent of any other instances in the current program. The parallel method of the SplittableRandom class creates an array of random pseudo integers that can be used.

package com.yawintutor;

import java.util.SplittableRandom;

public class RandomNumber {
	public static void main(String[] args) {
		RandomNumber r = new RandomNumber();
		System.out.println(r.random(5, 10));
	}
	public int random(int min, int max) {
		return new SplittableRandom().ints(min, min, max+1 ).parallel().findFirst().getAsInt();
	}
}

Output

8


5. Without using Random classes

The following example will not make use of any of Java’s random classes. The current timestamp is utilised to generate the random integer value in this technique. The current timestamp is converted to milliseconds, and the range of values is limited using the module function.

package com.yawintutor;

import java.util.Calendar;

public class RandomNumber {
	public static void main(String[] args) {
		RandomNumber r = new RandomNumber();
		System.out.println(r.random(5, 10));
	}
	public int random(int min, int max) {
		return (int)Calendar.getInstance().getTimeInMillis()%(max-min+1)+min;
	}
}

Output

6



Leave a Reply