Here you will learn to generate random number in java between two given number by different means. I will try to provide cons for different mechanism so that you can choose what is best for you.

## 1- Math.random()

This method will always return number between 0(inclusive) and 1(exclusive). Simple tweak can be used to generate random number between give two numbers.

```
package com.jbt.random;
import java.util.Random;
/*
* Generate random number between given high and low number.
*/
public class generate_random_between_two_number {
public static void main(String[] args) {
// It will generate random number between 0 and 50
System.out.println((int) (Math.random() * 50));
/*
* Below code will generate random number
* between 10 and 50. ((int)
* (Math.random()*(maximum - minimum))) +
* minimum
*
* This is simple tweak which is used
* here. First we generate random number
* between 0 and (maximum - minimum)(40 in
* current scenario) and then adding
* minimum number after random number gets
* generated.
*
*/
System.out.println(
((int) (Math.random() * (50 - 10))) + 10);
}
}
```

#### Bullet Points :

- Internally it uses java.util.Random() to generate random numbers.
- First time when called, it creates random number generator and for future calls it uses nextDouble() method from random class.
- Method is synchronized hence multiple thread is accessing this to generate numbers it may reduce contention for each thread to have its own pseudorandom-number generator.
- For better performance for multi threaded environment you can use ThreadLocalRandom introduced in JDK 1.7.
- As Math.random internally uses nextDouble method, it will always return double number.
- In order to get Integer you have to cast the value to integer.

## 2- java.util.Random

```
package com.jbt.random;
import java.util.Random;
/*
* Generate random number between given high and low number.
*/
public class generate_random_between_two_number {
public static void main(String[] args) {
Random randomObj = new Random();
/*
* Below code will generate random number
* between 10 and 50. ((int)
* (Math.random()*(maximum - minimum))) +
* minimum
*
* This is simple tweak which is used
* here. First we generate random number
* between 0 and (maximum - minimum)(40 in
* current scenario) and then adding
* minimum number after random number gets
* generated.
*
* Max value is exclusive in case of
* nextInt to make it inclusive you need
* to add 1.
*/
int randomNum = randomObj.nextInt((50 - 10)) + 10;
System.out.println(randomNum);
}
}
```

#### Bullet Points:

- Same as above, Here contention problem will occur in case of multi threaded environment. As random is only used in case of Math.random.
- To overcome performance problem you can use ThreadLocalRandom.
- As opposed to Math.random, Random class have multiple methods for generating different type of numbers(e.g. int/double/long etc).
- There are multiple algorithms that can be used by random class for generating random numbers.
- For portability it is necessary to let random class use all algorithms available.
- Random class is not final meaning it can extended and extended class can use any specific algorithms to generate random number.

## 3- ThreadLocalRandom

ThreadLocalRandom extends Random class and provide enhancement for multi threaded environment.

```
package com.jbt.random;
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandom_Example {
public static void main(String[] args) {
/*
* Returns random integer value between 0(Inclusive) and given value(exclusive).
*/
System.out.println(ThreadLocalRandom.current().nextInt(50));
// Returns random integer value between given numbers.
System.out.println(ThreadLocalRandom.current().nextInt(10, 50));
}
}
```

#### Bullet Point

- ThreadLocalRandom provides better performance than random in case of multi threaded environment.
- Explicit seed setting is not allowed.

## 4- Java 8

In java 8 some new methods have been included in Random class. Which can be used to generate random number without any hiccups. You can use Java 8 Lambda feature to get the result.

```
package com.jbt.random;
import java.util.Random;
/*
* Generate random integer between two given number using methods
* introduced in JDK 1.8.
*/
public class generate_random_between_two_number_jdk_8 {
public static void main(String[] args) {
Random randomObj = new Random();
System.out.println(randomObj.ints(10, 50).findFirst().getAsInt()); ;
}
}
```

## Arvind

Thanks!!

## Arvind

Rather than creating a class, I want to use the Math.random() between 2 specific numbers(say 48, 49). Any idea how can I do that?

## Abdullahi

min + (int) (Math.random() * (max-min)); // This generates random numbers from min(inclusive) to max(exclusive)

Therefore 48 + (int) (Math.random() * 2); //This generates random numbers between 48 and 49 (with both numbers being inclusive)