Java Random Number Generation random()


java.lang.Math class comes with many methods to do simple basic numeric operations. One such one is random() method which returns a pseudorandom number from 0.0 to less than 1.0. That is, the range of random() method generates is greater than or equal to 0.0 to less than 1.0 (exclusive of 1.0). The random numbers generated are always positive values.

One advantage of Math class methods is that they are declared as static so that they can be called without the need of object creation.

Following is the Math class signature as defined in java.lang package.

public final class Math extends Object

Following is the signature of random() method in Math class.

  • double random(): Returns a positive double value greater than or equal to 0.0 and less than 1.0.

Notice, the method does not take any parameter but returns a double value.

Example on Java Random Number Generation random() printing 10 random numbers in a for loop.
public class MathFunctionDemo
{
  public static void main(String args[])
  {
    for(int i = 0; i < 10; i++)
    {
      double randomNumber = Math.random();
      System.out.println(randomNumber);
    }
  }
}


Java Random Number Generation random()
Output screenshot on Java Random Number Generation random() when the code is run

To generate a range of whole random numbers using Math.random() method.

The following code prints 10 random integer numbers in the range 1 (inclusive) to 10 (inclusive).

public class MathFunctionDemo
{
  public static void main(String args[])
  {
    for(int i = 0; i < 10; i++)
    {
      int randomNumber = (int) (10.0 * Math.random()) + 1;
      System.out.println(randomNumber);
    }
  }
}


Java Random Number Generation random()
Output screen on Java Random Number Generation random() when code is run

10.0 * Math.random() scales the range from 0.0 to 9.99999. Casting to int returns integer values in the range 0 to 9. Note that the decimal value is truncated and not rounded. Addition of 1 makes the range 9+1, that is 10.

The above sytle is tedious. To generate random integer numbers within a range, the best and speedier way is using nextInt() method of java.util.Random class.

Let us dig more:

  1. The simplest way to generate random number is Math.random() method. It is equivalent to using Random class nextDouble() method.
  2. There are 3ways to generate random number in Java using the classes Random, ThreadLocalRandom (added in Java 7 or JDK 1.7), Math with random() method,
  3. Math.random() only generates double value within the range >= 0.0 to < 1.0
  4. Usage of Math.random() method is thread-safe and suitable in multithreaded environment. Using class ThreadLocalRandom from Java 1.7 is another choice for the usage in multiple threads access.

Full depth explanation is available at class Random to generate random numbers with seed or without seed and different sequence of random numbers or with the same sequence of random numbers.

Leave a Comment

Your email address will not be published.