V.2.1 The Random Function
Mathematica can be used to create random numbers with arbitrary probability distributions. The numbers can be integer, real, or complex. There are dozens of different distributions that you can use, or you can create your own.
First we demonstrate a series of random number generations. Here is a random integer between 1 and 100.
Here is a random integer in the range from -100 to 100.
By omitting the range, the default of 0 to 1 is used. Here are 10 random integers between 0 and 1.
Here is a real random number in the range from 1.1 to 5.2.
By omitting the range, you get a random number in the default range of 0 to 1.
In fact, by omitting the number type, we see that the default is for a real number in the range from 0 to 1.
V.2.2 Testing a random number generator
There are numerous ways to look at and test the randomness of a random number generator. A rather intuitive method might look at the distribution of the numbers over the range from which they are taken.
Here is a list of 1000 integers all taken between 0 and 9.
The two intuitive tests of randomness that we might be concerned with are the frequency with which each digit occurs, and the presence (or lack thereof) of any patterns in the sequence itself. If the above sequence contained approximately equal amounts of each digit but started off like the following sequence, we would be highly suspect.
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,...
The frequency with which each digit occurs in the list data, can be checked as follows:
ADVANCED V.2.2a Calculate the frequencies of the 'data' from above: Is this what you expected? Hint: perform your experiments as a function of the length of the sequence.
For a more careful analysis of random sequences, a variety of tests are available. One of the more commonly known tests for determining the effective randomness of a sequence, is the χ (chi-squared) test. This test determines how evenly spread the numbers appear in a sequence and uses their frequency of occurrence to come up with a statistic. If is the number of values in the list of a sequence of positive numbers, then for a uniform distribution, we would expect copies of each number. The χ test is given by the following formula:
Note that sums all the deviations from the expected values. In the formula, is the frequency of copies of in the sequence. If the statistic is close to the number of values of the bins , then the numbers are assumed to be reasonably random. Here is an implementation of the χ statistic.
In general, sequences of positive integers are considered sufficiently random if the χ statistic is given by the following table with degree of freedom = n-1: The confidence level of 95% depends on degrees of freedom dof = n-1 instead of n: when the computed chisquare does not exceed the confidence level listed below, the sequence is sufficiently random.
|
|
1
|
3.841`
|
2
|
5.991`
|
3
|
7.815`
|
4
|
9.488`
|
5
|
11.071`
|
6
|
12.592`
|
7
|
14.067`
|
8
|
15.507`
|
9
|
16.919`
|
10
|
18.307`
|
|