This article will explain 3 different methods to generate a random number in java in range.

Random number means a different number every time the application is executed.

Sounds interesting but have you thought why is it required.

Random number generator becomes handy when you want to create a unique name.

**Example**, your application creates a file every time it is executed. Obviously you will define the name of file which will be created in the code .

If the name of file will be the same every time, it will keep on overwriting which means that you will only be able to keep one file at a time.

What if you want to keep a record of this file for all executions of application.

Solution is to make the name of created file unique.

This can easily be done by generating a random number and appending it to the name of file initialized in the code.

**Java random number generator **Java provides many approaches to generate unique random numbers between a given range. These approaches are discussed below to create a random number generator program.

**All the approaches assume that the random number generated is in the range 1 to 50.**

**Method 1 : Using Math class **

`java.lang.Math`

class has a `random()`

method which generates a decimal value of type double which is greater than 0.0 and less than 1.0(0.9999), that is **in the range 0.0(inclusive) to 1.0(exclusive)**.

This value is different every time the method is invoked.

In order to generate a number between 1 to 50, we multiply the value returned by **Math.random() method** by 50.

This makes its range as 0.0 to 49.995. Now add 1 to it.

This increases its range as 1.0 to 50.995. If this result is cast to an `int`

, the range will be 1 to 50.

Below program demonstrates this algorithm and shows the result for different executions.

public class RandomNumberGenerator { public static void main(String[] args) { usingMathClass(); } static void usingMathClass() { double randomDouble = Math.random(); randomDouble = randomDouble * 50 + 1; int randomInt = (int) randomDouble; System.out.println(randomInt); } }

**Output**

Random number generated is : 50

Random number generated is : 27

Random number generated is : 32

Random number generated is : 21

**Method 2 : Using Random class **

`java.util.Random`

class provides a method `nextInt()`

which can generate random numbers between 0 and a specified upper boundary.This method takes an integer as argument.

This integer value represents the upper limit of the random number that will be generated by this method.

The random number generated will be between 0(inclusive) and the upper boundary value(exclusive).

In order to generate a random number between 1 and 50 we create an object of `java.util.Random`

class and call its `nextInt()`

method with 50 as argument.

This will generate a number between 0 and 49 and add 1 to the result which will make the range of the generated value as 1 to 50.

import java.util.Random; public class RandomNumberGenerator { public static void main(String[] args) { usingRandomClass(); } static void usingRandomClass() { Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(50) + 1; System.out.println("Random number is : " + randomInt); } }

**Output**

Random number generated is : 2

Random number generated is : 29

Random number generated is : 8

Random number generated is : 16

**Method 3 : Using ThreadLocalRandom class **

Java 7 has provided a new class called

`java.util.concurrent.ThreadLocalRandom`

. This class has methods to generate random numbers.This class is appropriate for generating random numbers in multithreaded environments since it will be a less overhead as compared to using `java.util.Random`

class.

`ThreadLocalRandom`

class has a method `nextInt()`

which takes two arguments representing the lower and upper boundary values and returns an integer between those values.

The result will be between lower boundary(inclusive) and upper boundary(exclusive). For generating a number between 1 and 50, the lower and upper bounds passed to this method will be 1 and 51 respectively.

In order to call this method, first its `current`

method is called, then `nextInt()`

is called as `ThreadLocalRandom.current().nextInt(1, 51);`

.

Program for generating random numbers using this approach is given below.

import java.util.concurrent.ThreadLocalRandom; public class RandomNumberGenerator { public static void main(String[] args) { usingThreadLocalClass(); } static void usingThreadLocalClass() { int randomInt = ThreadLocalRandom.current().nextInt(1, 51); System.out.println("Random number is : " + randomInt); } }

**Output**

Random number generated is : 11

Random number generated is : 1

Random number generated is : 35

Random number generated is : 21

**Let’s tweak in :**

- Upper boundary provided to
`randomInt()`

method of`java.util.Random`

class should be positive. If it is less than 0, the method throws a`java.lang.IllegalArgumentException`

. `java.lang.Math`

class uses`java.util.Random`

class’`nextDouble()`

method for random number generation.`java.util.concurrent.ThreadLocalRandom`

is a child class of`java.util.Random`

.`java.util.Random`

class provides methods to generate random numbers of different types such as`long`

,`float`

,`boolean`

,`double`

.- For generating a random number between 0 and 1 use
`random()`

method of`java.util.Math`

class(**Method 1**). `nextInt()`

method of`java.util.concurrent.ThreadLocalRandom`

with two consecutive numbers as lower and upper range will**always produce lower range as result.**That is,`nextInt(50, 51)`

will always give 50 as result.

**Not willing to read?**

*Watch the video*

Hope you liked this post and learnt something from it. If yes, then **hit the clap button below to show your appreciation**.