You are on page 1of 1

The Random class can be used in programs that need random numbers.

Random is part
of the java.util package. The numbers that Random generates are actually
pseudorandom, because they are calculated using a formula. But they appear to be
random and can be used in most situations that call for random numbers.

A Random object is started out with a seed value which determines the sequence of
numbers it will produce. Every Random object started with the same seed value will
produce the same sequence of numbers. There are two constructors for Random
objects.

The first constructor uses a seed value based on the current time in milliseconds.
Use it when you need a different stream of random numbers for each run of the
program. For example, a game program that uses random numbers to simulate a
shuffled deck of cards needs a different stream each time the game is played.

The second constructor gives you the ability to repeat a pseudorandom sequence. Use
it when you need to test different strategies against the same stream of random
events.

Here is an example of constructing a random number generator:

Random rand = new Random();


Now the methods of the Random class can be used with rand.

You might also like