
We have already used the Random class a little, but let's investigate this in more detail. The class Random enables you to create multiple random number generators that are independent of one another. Each object of the class is a separate random number generator. Any Random object can generate pseudorandom numbers of types int, long, float, or double. These numbers are created using an algorithm that takes a 'seed' and 'grows' a sequence of numbers from it. Initializing the algorithm twice with the same seed would produce the same sequence because the algorithm is deterministic.
The integer values generated will be uniformly distributed over the complete range for the type, and the floating point values will be uniformly distributed over the range 0.0 to 1.0 for both types. You can also generate numbers of type double with a Gaussian (or normal) distribution that has a mean of 0.0 and a standard deviation of 1.0. This is the typical bellshaped curve that represents the probability distribution for many random events.
There are two constructors for a Random object. The default constructor will create an object that uses the current time from your computer clock as the seed value for generating pseudorandom numbers. The other constructor accepts an argument of type long that will be used as the seed.
Random lottery = new Random(); // Sequence not repeatable Random repeatable = new Random(997L); // Repeatable sequence
If you use the default constructor, the sequence of numbers that is generated will be different each time a program is run, although beware of creating two generators in the same program with the default constructor. The time resolution used is one millisecond, so if you create two objects in successive statements they will usually generate the same sequence because the times used for the starting seed values will be identical. Random objects created using the same seed will always produce the same sequence, which can be very important when you are testing a program. Testing a program where the output is not repeatable can be a challenge! A major feature of random number generators created using a given seed in Java is that not only will they always produce the same sequence of pseudorandom numbers from a given seed, but they will also do so even on totally different computers.
The public methods provided by a Random object are:
To produce a pseudorandom number of a particular type, you just call the appropriate method for a Random object. You can repeat the sequence of numbers generated by a Random object that you created with a seed value, by calling the setSeed() method with the same seed value as an argument.
We can give the Random class an outing with a simple program that simulates throwing a pair of dice. We'll assume you get six throws to try to get a double six.
Here's the program:
import java.util.Random; import java.io.IOException; public class Dice { public static void main(String[] args) { System.out.println("You have six throws of a pair of dice.\n" + "The objective is to get a double six. Here goes...\n"); Random diceValues = new Random(); // Random number generator String[] theThrow = {"First ", "Second ", "Third ", "Fourth ", "Fifth ", "Sixth "}; int die1 = 0; // First die value int die2 = 0; // Second die value for(int i = 0; i < 6; i++) { die1 = 1 + diceValues.nextInt(6); // Number from 1 to 6 die2 = 1 + diceValues.nextInt(6); // Number from 1 to 6 System.out.println(theThrow[i] + "throw: " + die1 + ", " + die2); if(die1 + die2 == 12) { // Is it double 6? System.out.println(" You win!!"); // Yes !!! return; } } System.out.println("Sorry, you lost..."); return; } }
If you compile this program you should get output that looks something like this:
You have six throws of a pair of dice. The objective is to get a double six. Here goes... First throw: 3, 2 Second throw: 1, 1 Third throw: 1, 2 Fourth throw: 5, 3 Fifth throw: 2, 2 Sixth throw: 6, 4 Sorry, you lost...
How It Works
We use one random number generator here that we create using the default constructor, so it will be seeded with the current time and will produce a different sequence of values each time the program is run. We simulate throwing the dice in the for loop. For each throw we need a random number between 1 and 6 for each die. The easiest way to produce this is to add one to the value returned by the nextInt() method when we pass 6 as the argument. If we wanted to make a meal of it we could obtain the same result by using the statement:
die1 = 1 + abs(diceValues.nextInt())%6; // Number from 1 to 6
Remember that the pseudorandom integer values that we get from the nextInt() method will be uniformly distributed across the whole range of possible values for type int, positive and negative. That's why we need to use the abs() method from the Math class here to make sure we end up with a positive die value. The remainder after dividing the value resulting from abs(diceValues.nextInt()) by 6 will be between 0 and 5. Adding 1 to this produces the result we want.
Note 
Remember that the odds against a double six are 36:1, so you'll only succeed once on average out of every six times you run the example. 
Now we'll move on to look at dates and times.