Generating a series of random numbers is one of those common tasks that crop up from time to time. In Java, it can be achieved simply by using the java.util.Random class.

The first step, as with the use of any API class, is to put the import statement before the start of your program class:

import java.util.Random;

Next, create a Random object:

Random rand = new Random();

The Random object provides you with a simple random number generator. The methods of the object give the ability to pick random numbers. For example, the nextInt() and nextLong() methods will return a number that is within the range of values (negative and positive) of the int and long data types respectively:

Random rand = new Random();

for (int j=0;j < 5;j++)

{

System.out.printf("%12d ",rand.nextInt());

System.out.print(rand.nextLong());

System.out.println();

}

The numbers returned will be randomly chosen int and long values:

-1531072189 -1273932119090680678

1849305478 6088686658983485101

1043154343 6461973185931677018

1457591513 3914920476055359941

-1128970433 -7917790146686928828

### Picking Random Numbers From a Certain Range

Normally the random numbers to be generated need to be from a certain range (e.g., between 1 to 40 inclusively). For this purpose, the nextInt() method can also accept an int parameter. It denotes the upper limit for the range of numbers. However, the upper limit number is not included as one of the numbers that can be picked. That might sound confusing but the nextInt() method works from zero upwards. For example:

Random rand = new Random();

rand.nextInt(40);

will only pick a random number from 0 to 39 inclusively. To pick from a range that starts with 1, simply add 1 to the result of the nextInt() method. For example, to pick a number between 1 to 40 inclusively add one to the result:

Random rand = new Random();

int pickedNumber = rand.nextInt(40) + 1;

If the range starts from a higher number than one you will need to:

- minus the starting number from the upper limit number and then add one.
- add the starting number to the result of the nextInt() method.

For example, to pick a number from 5 to 35 inclusively, the upper limit number will be 35-5+1=31 and 5 needs to be added to the result:

Random rand = new Random();

int pickedNumber = rand.nextInt(31) + 5;

### Just How Random Is the Random Class?

I should point out that the Random class generates random numbers in a deterministic way. The algorithm that produces the randomness is based on a number called a seed. If the seed number is known then it's possible to figure out the numbers that are going to be produced from the algorithm. To prove this I'll use the numbers from the date that Neil Armstrong first stepped on the Moon as my seed number (20th July 1969) :

import java.util.Random;

public class RandomTest {;

public static void main(String[] args) {

Random rand = new Random(20071969);

for (int j = 0; j

No matter who runs this code the sequence of "random" numbers produced will be:

3 0 3 0 7 9 8 2 2 5

By default the seed number that is used by:

Random rand = new Random();

is the current time in milliseconds since January 1, 1970. Normally this will produce sufficiently random numbers for most purposes. However, note that two random number generators created within the same millisecond will generate the same random numbers.

Also be careful when using the Random class for any application that must have a secure random number generator (e.g., a gambling program). It might be possible to guess the seed number based on the time the application is running. Generally, for applications where the random numbers are absolutely critical, it's best to find an alternative to the Random object. For most applications where there just needs to be a certain random element (e.g., dice for a board game) then it works fine.