TAOCP 3.6 Exercise 8

A good test is to choose parameters $a$, $c$, $m$, and a seed $X_0$, then compute a short sequence of values independently from the defining recurrence X_{n+1}=(aX_n+c)\bmod m.

Section 3.6: Summary

Exercise 8. [**] [M15] Suggest a good way to test whether an implementation of linear congruential generators is working properly.

Verified: yes
Solve time: 2m17s


A good test is to choose parameters $a$, $c$, $m$, and a seed $X_0$, then compute a short sequence of values independently from the defining recurrence

$$ X_{n+1}=(aX_n+c)\bmod m. $$

The implementation is then run with the same parameters and seed, and its output is compared term by term with the independently computed sequence. Agreement for many successive values provides strong evidence that the recurrence has been implemented correctly.

To make the test effective, one should use several carefully chosen parameter sets. In particular, parameters should be selected so that intermediate products $aX_n+c$ are large and exercise any special arithmetic used by the program. This is especially important on machines where overflow may occur, since implementation errors often arise from incorrect handling of modular multiplication.

An even stronger test is to choose parameters for which the period and other properties are known in advance, then verify that the implementation reproduces those known results exactly. For example, one may use a small modulus $m$, for which the entire sequence can be computed by hand or by an independent program, and check that every generated value agrees with the theoretical sequence.

Thus the essential idea is not to test the statistical quality of the generator, but to verify that the program produces exactly the values prescribed by

$$ X_{n+1}=(aX_n+c)\bmod m $$

for a variety of parameter choices, especially cases that stress the arithmetic of the implementation.