Generate random numbers 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.

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

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

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 :

  1. 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.
  2. java.lang.Math class uses java.util.Random class’ nextDouble() method for random number generation.
  3. java.util.concurrent.ThreadLocalRandom is a child class of java.util.Random.
  4. java.util.Random class provides methods to generate random numbers of different types such as long, float, boolean, double.
  5. For generating a random number between 0 and 1 use random() method of java.util.Math class(Method 1).
  6. 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.

Leave a Reply