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:

Next, create a Random object:

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:

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

### 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:

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:

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:

### 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) :

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

By default the seed number that is used by:

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.