# Generating random integers in a range

Follow me

### Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.
Follow me

#### Latest posts by Vinay Singh (see all)

In this post I will be discussing how to generate random integers in a range. Random integers can be used for simple games and simulations. In Java random (actually pseudorandom) numbers can be generated through Math.random() function or using the Random class of java.util package. Math.random() generates double value and since this post is about generating random integers in a range we would be using the Random class.

The following code snippet would generate a pseudorandom integer in the entire integer range, that is all 232 possible int values are produced with (approximately) equal probability:

import java.util.*;
class RandomIntegers{
public static void main(String []args){
Random objRandom = new Random();
int randomValue;
randomValue = objRandom.nextInt();
System.out.println(randomValue);
}
}

The function nextInt(int n) returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), that is [0, ). All possible int values are produced with (approximately) equal probability. The following code snippet would generate a pseudorandom integer in the range [0, 10):

import java.util.*;
class RandomIntegers{
public static void main(String []args){
Random objRandom = new Random();
int randomValue;
randomValue = objRandom.nextInt(10);
System.out.println(randomValue);
}
}

Now, let us say that we want to generate a random integer between min and max (both inclusive). If we evaluate

randomValue = objRandom.nextInt(max);

it would generate numbers between 0 to one less than max, that is we need to add one to the above to include the possibility of max also. Now

randomValue = objRandom.nextInt(max+1);

would give integers from 0 to max. We want that integers should start from min, so we must add min to make the result start from atleast min like follows:

randomValue = min + (objRandom.nextInt(max+1));

But, now the result the upper limit would also shift by min and needs to be compensated as follows:

randomValue = min + (objRandom.nextInt(max-min+1));

The complete Java program to generating random integers in a range with a separate function exclusively for generating random integers in a range of max and min is as follows:
import java.util.*;

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

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.

Visit my other post Random number generator if you are interested in generating (pseudo) random number without using the function provided by the Java API.