2 and 12, since there is only one way to get each of those sums.
Random
Class
Random()
— Creates a new random number generator using a seed based on the current time.
Random(long seed)
— Creates a new random number generator based on a specific seed value.
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
.
Are there situations where you do want to use the exact same "random" sequence for several runs of a program?