Overview

This article describes how to generate random numbers in Java. There are multiple ways available to generate random numbers and based on the needs either one can be used.

Method 1 - Using Java's Random

One of the simplest ways is to use java.lang.Math.random() method. It generates double ranging from equals to or greater than 0.0 and less than 1.0.

0.0 >= range < 1.0

public double getRandomNumber() { return Math.random(); }

In case somebody wants to generate random numbers between custom range, above java example can be modified to transform the generated number to the specified range.

__Example – Generate random numbers between 0 and 100__

This can be achieved by simply multiplying the output of Math.random() with 100.

public double getRandomNumber() { return Math.random()*100; }

__Example – Generate random numbers between custom range__

**Option 1** – It can be achieved in different ways. One way would be to convert the numbers generated between 0 and 1 to custom range. For example if numbers are to be generated between 5 and 9, numbers generated from Math.random() can be multiplied with the range and add into the min number in the range.

public double getRandomNumber() { double number = Math.random()*100; int minNumber = 5; int maxNumber = 9; return (number*(maxNumber - minNumber))+minNumber; }

This method would generate random numbers in custom range exclusive of max number.

**Option 2** – java.util.Random class can be used to generate random numbers between custom range. It provides method to provide the bound which can be used to generate numbers between custom range.

// random integers exclusive of max number public int getRandomNumber() { Random random = new Random(); int minNumber = 5; int maxNumber = 9; int number = minNumber + random.nextInt(maxNumber-minNumber); return number; }

Above method generates integer random numbers in custom range exclusive of max number.

// random integers inclusive of max number public int getRandomNumber() { Random random = new Random(); int minNumber = 5; int maxNumber = 9; int number = minNumber + random.nextInt(maxNumber-minNumber +1); return number; }

Above method generates integer random numbers in custom range inclusive of max number.

Method 2 - Using Apache math utility method

// random integers inclusive of max number public int getRandomNumber() { RandomDataImpl dataGenerator = new RandomDataImpl(); return dataGenerator.nextInt(5, 9); }

### Uniformness Test

Below test is performed to test the uniformness of the random numbers generated using different methods. Given these are random numbers and test results would vary when performed at different times, this is just to show how numbers are uniformally distributed when tests were performed-

__Method 1__ – Different data set of numbers generated between 0.0 and 1.0 using Math.random() method

Data set – 10 Random numbers

As you see below with small data set of 10 random numbers generated using Math.random() method, there are no numbers been generated between 0.0 and 0.2.

Data set – 50 Random numbers

As you see below random numbers are more uniformally distributed with a data set of 50 random numbers

Data set – 500 random numbers

When large set of random numbers are generated, they are more uniformally distributed __Method 1__ – Different data set of integers generated between 5 and 15 using Apache common’s RandomDataImpl class

Data set – 10 Random numbers

As you see below with small data set, numbers are not uniformly distributed and are concentrated towards number 6.

Data set – 50 Random numbers

With 50 numbers generated between 5 and 15, numbers are more uniformally distributed

Data set – 500 random numbers

Similar to what we saw with Math.random method, numbers are more uniformally distributed in large data set.

References

https://commons.apache.org/proper/commons-math/

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

{jcomments on}