16.7: Random Numbers
- Page ID
- 18916
Most computer programs do the same thing every time they run; programs like that are deterministic. Usually determinism is a good thing, since we expect the same calculation to yield the same result. But for some applications, we want the computer to be unpredictable. Games are an obvious example, but there are many others.
Making a program nondeterministic turns out to be hard, because it’s hard for a computer to generate truly random numbers. But there are algorithms that generate unpredictable sequences called pseudorandom numbers. For most applications, they are as good as random.
If you did Exercise 3.12.4, you have already seen java.util.Random
, which generates pseudorandom numbers. The method nextInt
takes an integer argument, n
, and returns a random integer between 0
and n - 1
(inclusive).
If you generate a long series of random numbers, every value should appear, at least approximately, the same number of times. One way to test this behavior of nextInt
is to generate a large number of values, store them in an array, and count the number of times each value occurs.
The following method creates an int
array and fills it with random numbers between 0 and 99. The argument specifies the size of the array, and the return value is a reference to the new array.
public static int[] randomArray(int size) { Random random = new Random(); int[] a = new int[size]; for (int i = 0; i < a.length; i++) { a[i] = random.nextInt(100); } return a; }
The following fragment generates an array and displays it using printArray
from Section 8.3:
int numValues = 8; int[] array = randomArray(numValues); printArray(array);
The output looks like this:
{15, 62, 46, 74, 67, 52, 51, 10}
If you run it, you will probably get different values.