<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=AOCP%2FRandom_Numbers</id>
	<title>AOCP/Random Numbers - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=AOCP%2FRandom_Numbers"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=AOCP/Random_Numbers&amp;action=history"/>
	<updated>2026-06-20T08:49:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=AOCP/Random_Numbers&amp;diff=30735&amp;oldid=prev</id>
		<title>Admin: Create AOCP/Random Numbers page covering Knuth Volume 2 Chapter 3: LCGs, spectral test, statistical tests, random sampling, and shuffling (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=AOCP/Random_Numbers&amp;diff=30735&amp;oldid=prev"/>
		<updated>2026-06-20T04:56:06Z</updated>

		<summary type="html">&lt;p&gt;Create AOCP/Random Numbers page covering Knuth Volume 2 Chapter 3: LCGs, spectral test, statistical tests, random sampling, and shuffling (via create-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Knuth Volume 2: Seminumerical Algorithms=&lt;br /&gt;
&lt;br /&gt;
==Chapter 3: Random Numbers==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
Random numbers are essential for many computer applications: simulation, sampling, numerical analysis, computer programming (as a source of arbitrary data for testing), decision-making, and cryptography. Knuth observes that random numbers should not be generated &amp;quot;by a random method.&amp;quot; Instead, we require a deterministic procedure that produces numbers satisfying useful statistical tests of randomness — hence the term &amp;#039;&amp;#039;pseudo-random numbers&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The chapter treats four main topics:&lt;br /&gt;
* Generating uniform random numbers (the fundamental building block)&lt;br /&gt;
* Statistical tests for verifying randomness&lt;br /&gt;
* Techniques for transforming uniform variates into other distributions&lt;br /&gt;
* The philosophical question: what &amp;#039;&amp;#039;is&amp;#039;&amp;#039; a random sequence?&lt;br /&gt;
&lt;br /&gt;
===Generating Uniform Random Numbers===&lt;br /&gt;
&lt;br /&gt;
A uniform random number generator produces a sequence of numbers that behave as if each were drawn independently from the uniform distribution on the interval [0, 1). The standard approach is to generate integers &amp;lt;math&amp;gt;X_n&amp;lt;/math&amp;gt; in the range &amp;lt;math&amp;gt;0 \leq X_n &amp;lt; m&amp;lt;/math&amp;gt; and then output the scaled value &amp;lt;math&amp;gt;U_n = X_n / m&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====The Linear Congruential Method====&lt;br /&gt;
&lt;br /&gt;
The linear congruential generator (LCG) is the most widely studied and applied method. It is defined by the recurrence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
X_{n+1} = (a X_n + c) \bmod m, \qquad n \geq 0&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
m &amp;amp;&amp;gt; 0 \quad \text{— the modulus} \\&lt;br /&gt;
0 &amp;amp;&amp;lt; a &amp;lt; m \quad \text{— the multiplier} \\&lt;br /&gt;
0 &amp;amp;\leq c &amp;lt; m \quad \text{— the increment} \\&lt;br /&gt;
0 &amp;amp;\leq X_0 &amp;lt; m \quad \text{— the seed (starting value)}&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence is entirely determined by the four parameters &amp;lt;math&amp;gt;(m, a, c, X_0)&amp;lt;/math&amp;gt;. The scaled output is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
U_n = \dfrac{X_n}{m} \in [0, 1)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt;, the generator is called a &amp;#039;&amp;#039;multiplicative congruential generator&amp;#039;&amp;#039; (or Lehmer generator). When &amp;lt;math&amp;gt;c \neq 0&amp;lt;/math&amp;gt;, it is called a &amp;#039;&amp;#039;mixed congruential generator&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The sequence repeats after a finite number of steps — the &amp;#039;&amp;#039;period&amp;#039;&amp;#039; of the generator. A generator with period &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; (when &amp;lt;math&amp;gt;c \neq 0&amp;lt;/math&amp;gt;) or &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; (when &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; is prime) is said to have &amp;#039;&amp;#039;full period&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=====Choice of Modulus=====&lt;br /&gt;
&lt;br /&gt;
The modulus &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; should be as large as possible, since the period cannot exceed &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;. Practical considerations:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Power of two:&amp;#039;&amp;#039;&amp;#039; Let &amp;lt;math&amp;gt;m = 2^e&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;e&amp;lt;/math&amp;gt; is the word size of the computer (e.g., &amp;lt;math&amp;gt;2^{31}&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;2^{64}&amp;lt;/math&amp;gt;). The modulo operation reduces to bit truncation, which is extremely fast. Disadvantage: lower-order bits have short periods.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Prime modulus:&amp;#039;&amp;#039;&amp;#039; Let &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; be a large prime. Gives better randomness properties in the low-order bits. Disadvantage: the modulo operation requires an explicit division step, though Schrage&amp;#039;s method can avoid double-width products.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Mersenne primes:&amp;#039;&amp;#039;&amp;#039; Using &amp;lt;math&amp;gt;m = 2^p - 1&amp;lt;/math&amp;gt; (Mersenne prime) combines some benefits of both approaches; e.g., &amp;lt;math&amp;gt;m = 2^{31} - 1&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=====Choice of Multiplier=====&lt;br /&gt;
&lt;br /&gt;
The choice of multiplier &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is critical to the quality of the generator.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hull–Dobell Theorem (full period for &amp;lt;math&amp;gt;c \neq 0&amp;lt;/math&amp;gt;):&amp;#039;&amp;#039;&amp;#039; A linear congruential generator has period &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; if and only if:&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;math&amp;gt;\gcd(c, m) = 1&amp;lt;/math&amp;gt;&lt;br /&gt;
# &amp;lt;math&amp;gt;a \equiv 1 \pmod{p}&amp;lt;/math&amp;gt; for every prime factor &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;&lt;br /&gt;
# &amp;lt;math&amp;gt;a \equiv 1 \pmod{4}&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;4 \mid m&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Full period for &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; prime:&amp;#039;&amp;#039;&amp;#039; The period is &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; if and only if &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is a primitive element modulo &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt; (i.e., &amp;lt;math&amp;gt;a^k \not\equiv 1 \pmod{m}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;1 \leq k &amp;lt; m-1&amp;lt;/math&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Full period for &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;m = 2^e&amp;lt;/math&amp;gt;:&amp;#039;&amp;#039;&amp;#039; The maximum period is &amp;lt;math&amp;gt;m/4 = 2^{e-2}&amp;lt;/math&amp;gt;, achieved when &amp;lt;math&amp;gt;a \equiv \pm 3 \pmod{8}&amp;lt;/math&amp;gt; and the seed &amp;lt;math&amp;gt;X_0&amp;lt;/math&amp;gt; is odd.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Additional desiderata:&amp;#039;&amp;#039;&amp;#039; Beyond achieving maximum period, the multiplier should:&lt;br /&gt;
* Produce good results on the spectral test (see below)&lt;br /&gt;
* Avoid values too close to &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt; or &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;&lt;br /&gt;
* Satisfy the condition that &amp;lt;math&amp;gt;a-1&amp;lt;/math&amp;gt; is not excessively divisible by prime factors of &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Knuth recommends multipliers that pass the spectral test in dimensions 2 through 6. Some well-known good multipliers:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;math&amp;gt;a = 16807&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;m = 2^{31}-1&amp;lt;/math&amp;gt; (MINSTD / Park–Miller)&lt;br /&gt;
* &amp;lt;math&amp;gt;a = 48271&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;m = 2^{31}-1&amp;lt;/math&amp;gt; (improved MINSTD)&lt;br /&gt;
* &amp;lt;math&amp;gt;a = 6364136223846793005&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;m = 2^{64}&amp;lt;/math&amp;gt; (MMIX)&lt;br /&gt;
&lt;br /&gt;
=====Potency=====&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;potency&amp;#039;&amp;#039; of a linear congruential sequence is defined as the smallest integer &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; such that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
(a-1)^s \equiv 0 \pmod{m}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The concept captures how much mixing the multiplier provides. A higher potency indicates that successive values are less correlated. For a modulus &amp;lt;math&amp;gt;m = 2^e&amp;lt;/math&amp;gt;, the potency is &amp;lt;math&amp;gt;s = \lceil e / \nu_2(a-1) \rceil&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;\nu_2(n)&amp;lt;/math&amp;gt; is the exponent of 2 dividing &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For good randomness, the potency should be at least 5 or 6. When &amp;lt;math&amp;gt;a \equiv 5 \pmod{8}&amp;lt;/math&amp;gt; (so &amp;lt;math&amp;gt;a-1&amp;lt;/math&amp;gt; is divisible by 4 but not by 8), the potency is maximized at &amp;lt;math&amp;gt;s = e/2&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If the potency is too low, the generator exhibits strong serial correlation. For example, &amp;lt;math&amp;gt;a = 1&amp;lt;/math&amp;gt; gives potency &amp;lt;math&amp;gt;s=1&amp;lt;/math&amp;gt; and produces a simple counter (not random at all).&lt;br /&gt;
&lt;br /&gt;
====Other Methods====&lt;br /&gt;
&lt;br /&gt;
Knuth also surveys alternatives to the linear congruential method:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Additive number generators:&amp;#039;&amp;#039;&amp;#039; Defined by &amp;lt;math&amp;gt;X_n = (X_{n-\ell} + X_{n-k}) \bmod m&amp;lt;/math&amp;gt;. These generalize the Fibonacci recurrence to a larger lag. Attractive for very long periods.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Lagged Fibonacci generators:&amp;#039;&amp;#039;&amp;#039; Use operations other than addition, such as multiplication or XOR.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Linear feedback shift registers (LFSRs):&amp;#039;&amp;#039;&amp;#039; Based on arithmetic in &amp;lt;math&amp;gt;\mathrm{GF}(2)[x]&amp;lt;/math&amp;gt;. All bits are full-period, avoiding the low-bit weakness of power-of-two LCGs. The Mersenne Twister is a modern descendant.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Combined generators:&amp;#039;&amp;#039;&amp;#039; Summing or shuffling outputs from multiple independent generators can increase period and improve statistical properties. Example: the Wichmann–Hill generator combines three LCGs.&lt;br /&gt;
&lt;br /&gt;
===Statistical Tests===&lt;br /&gt;
&lt;br /&gt;
A generator that produces numbers deterministically cannot be &amp;quot;truly random.&amp;quot; Instead, we subject it to statistical tests and declare it &amp;quot;pseudo-random&amp;quot; if it passes a battery of tests designed to detect common non-random behaviors.&lt;br /&gt;
&lt;br /&gt;
====General Test Procedures====&lt;br /&gt;
&lt;br /&gt;
Every statistical test follows the same pattern:&lt;br /&gt;
&lt;br /&gt;
# State a null hypothesis &amp;lt;math&amp;gt;H_0&amp;lt;/math&amp;gt;: &amp;quot;the sequence is random.&amp;quot;&lt;br /&gt;
# Select a test statistic &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; that measures some departure from randomness.&lt;br /&gt;
# Compute the theoretical distribution of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; under &amp;lt;math&amp;gt;H_0&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Observe the value &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; from the actual generator output.&lt;br /&gt;
# Reject &amp;lt;math&amp;gt;H_0&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;P(T \geq t \mid H_0)&amp;lt;/math&amp;gt; is too small (typically &amp;lt; 0.01 or &amp;gt; 0.99).&lt;br /&gt;
&lt;br /&gt;
Knuth recommends applying many different tests, since a generator may pass one test while failing another. A generator that passes test A, test B, and test C is more trustworthy than one that passes only test A.&lt;br /&gt;
&lt;br /&gt;
====Empirical Tests====&lt;br /&gt;
&lt;br /&gt;
Empirical tests apply statistical procedures directly to the numbers produced by the generator. No knowledge of the generator&amp;#039;s internal structure is used.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Equidistribution (frequency) test:&amp;#039;&amp;#039;&amp;#039; Does each possible value appear equally often? Use the chi-squared test or Kolmogorov–Smirnov test.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\chi^2 = \sum_{s=1}^{k} \dfrac{ (Y_s - \frac{n}{k})^2 }{ \frac{n}{k} }&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;math&amp;gt;Y_s&amp;lt;/math&amp;gt; is the observed count in category &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is the total number of observations. Under &amp;lt;math&amp;gt;H_0&amp;lt;/math&amp;gt;, this follows a chi-squared distribution with &amp;lt;math&amp;gt;k-1&amp;lt;/math&amp;gt; degrees of freedom.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Serial test:&amp;#039;&amp;#039;&amp;#039; Do pairs of consecutive numbers distribute uniformly in the unit square? Generalizes to triples, quadruples, etc.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Gap test:&amp;#039;&amp;#039;&amp;#039; Examine the lengths of gaps between occurrences of numbers in a specified range. The gap lengths should follow a geometric distribution.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Poker test (partition test):&amp;#039;&amp;#039;&amp;#039; Group &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; random numbers into hands of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; cards each and count the frequency of each poker-hand pattern. Classical version uses &amp;lt;math&amp;gt;k = 5&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Coupon collector&amp;#039;s test:&amp;#039;&amp;#039;&amp;#039; Observe how many random numbers must be generated to obtain a complete set of values from 1 to &amp;lt;math&amp;gt;d&amp;lt;/math&amp;gt;. The distribution of the waiting time has a known form.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Permutation test:&amp;#039;&amp;#039;&amp;#039; Divide the sequence into &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; groups of &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; elements; check whether the &amp;lt;math&amp;gt;t!&amp;lt;/math&amp;gt; possible relative orderings occur with equal frequency.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Run test:&amp;#039;&amp;#039;&amp;#039; Count the number of monotone runs (ascending or descending subsequences). Their lengths should follow a known distribution.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Collision test:&amp;#039;&amp;#039;&amp;#039; How many numbers must be generated before a duplicate appears? Related to the birthday paradox.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Maximum-of-&amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; test:&amp;#039;&amp;#039;&amp;#039; The distribution of the maximum of &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; consecutive uniform variates is &amp;lt;math&amp;gt;\Pr(\max \leq x) = x^t&amp;lt;/math&amp;gt;. Apply the KS test to observed maxima.&lt;br /&gt;
&lt;br /&gt;
====Theoretical Tests====&lt;br /&gt;
&lt;br /&gt;
Theoretical tests use knowledge of the generator&amp;#039;s recurrence to predict its global behavior, without actually running it.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Serial correlation test:&amp;#039;&amp;#039;&amp;#039; Compute the covariance between &amp;lt;math&amp;gt;X_n&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;X_{n+1}&amp;lt;/math&amp;gt;. For LCGs, the serial correlation coefficient &amp;lt;math&amp;gt;\rho_1&amp;lt;/math&amp;gt; can be bounded analytically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\rho_1 \approx \frac{1}{a} - \frac{6c}{am} \left(1 - \frac{c}{m}\right) + \mathcal{O}\left(\frac{a}{m}\right)&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Low serial correlation is desirable.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Lattice structure:&amp;#039;&amp;#039;&amp;#039; The set of all &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;-tuples &amp;lt;math&amp;gt;(X_n, X_{n+1}, \dots, X_{n+t-1})&amp;lt;/math&amp;gt; from an LCG lies on a lattice of equidistant parallel hyperplanes (Marsaglia&amp;#039;s theorem). This inherent structure is the LCG&amp;#039;s principal theoretical weakness.&lt;br /&gt;
&lt;br /&gt;
=====The Spectral Test=====&lt;br /&gt;
&lt;br /&gt;
The spectral test is the most powerful theoretical test for LCGs. For a given dimension &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;, it measures the maximum distance &amp;lt;math&amp;gt;1/\nu_t&amp;lt;/math&amp;gt; between adjacent parallel hyperplanes that cover all &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt;-tuples.&lt;br /&gt;
&lt;br /&gt;
Formally, for an LCG with period &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;, define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\nu_t = \min \left\{ \sqrt{x_1^2 + x_2^2 + \dots + x_t^2} \;\middle|\; x_1 + a x_2 + \dots + a^{t-1} x_t \equiv 0 \pmod{m}, \; (x_1, \dots, x_t) \neq (0, \dots, 0) \right\}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The figure of merit is &amp;lt;math&amp;gt;\nu_t&amp;lt;/math&amp;gt; (larger is better). A good generator should have &amp;lt;math&amp;gt;\nu_t \geq 0.1 m^{1/t}&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;t = 2, 3, \dots, 6&amp;lt;/math&amp;gt;. Knuth provides extensive tables of spectral test results for various multipliers.&lt;br /&gt;
&lt;br /&gt;
The spectral test reveals that RANDU (&amp;lt;math&amp;gt;a = 65539&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;m = 2^{31}&amp;lt;/math&amp;gt;) is catastrophically bad: in 3 dimensions, all points fall on just 15 planes.&lt;br /&gt;
&lt;br /&gt;
===Other Types of Random Quantities===&lt;br /&gt;
&lt;br /&gt;
Once a source of uniform random numbers is established, we can transform them into other distributions.&lt;br /&gt;
&lt;br /&gt;
====Numerical Distributions====&lt;br /&gt;
&lt;br /&gt;
Knuth covers methods for generating random variates from non-uniform distributions:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Inverse transformation method:&amp;#039;&amp;#039;&amp;#039; If &amp;lt;math&amp;gt;F(x)&amp;lt;/math&amp;gt; is the CDF, and &amp;lt;math&amp;gt;U&amp;lt;/math&amp;gt; is uniform on [0,1), then &amp;lt;math&amp;gt;X = F^{-1}(U)&amp;lt;/math&amp;gt; has distribution &amp;lt;math&amp;gt;F&amp;lt;/math&amp;gt;. Example: for the exponential distribution, &amp;lt;math&amp;gt;X = -\lambda \ln U&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Rejection method (von Neumann):&amp;#039;&amp;#039;&amp;#039; To generate from density &amp;lt;math&amp;gt;f(x)&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;[a,b]&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;0 \leq f(x) \leq M&amp;lt;/math&amp;gt;: generate &amp;lt;math&amp;gt;X \sim \text{Uniform}(a,b)&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;Y \sim \text{Uniform}(0,M)&amp;lt;/math&amp;gt;; accept &amp;lt;math&amp;gt;X&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;Y \leq f(X)&amp;lt;/math&amp;gt;, otherwise try again.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Normal distribution:&amp;#039;&amp;#039;&amp;#039; Several methods exist. The Box–Muller method generates two independent standard normal variates from two uniform variates:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
Z_1 &amp;amp;= \sqrt{-2 \ln U_1} \cos(2 \pi U_2) \\&lt;br /&gt;
Z_2 &amp;amp;= \sqrt{-2 \ln U_1} \sin(2 \pi U_2)&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The polar method (Marsaglia) avoids trigonometric evaluation and is generally faster.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Discrete distributions:&amp;#039;&amp;#039;&amp;#039; An efficient algorithm for generating from a discrete distribution with probabilities &amp;lt;math&amp;gt;p_0, p_1, \dots, p_{n-1}&amp;lt;/math&amp;gt; uses the &amp;quot;alias method&amp;quot; (Walker), which requires &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; preprocessing and &amp;lt;math&amp;gt;\mathcal{O}(1)&amp;lt;/math&amp;gt; time per sample.&lt;br /&gt;
&lt;br /&gt;
====Random Sampling and Shuffling====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Random sampling without replacement:&amp;#039;&amp;#039;&amp;#039; To select &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; items uniformly from a set of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Algorithm S (selection sampling):&amp;#039;&amp;#039;&amp;#039; Process items one by one; each item is selected with probability equal to (remaining needed) / (remaining items). Runs in &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; time.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Reservoir sampling:&amp;#039;&amp;#039;&amp;#039; When &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is unknown in advance (streaming data), maintain a reservoir of &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; candidates. Each new item replaces a random reservoir element with probability &amp;lt;math&amp;gt;k/t&amp;lt;/math&amp;gt;, where &amp;lt;math&amp;gt;t&amp;lt;/math&amp;gt; is the number of items seen so far.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Fisher–Yates shuffle (Algorithm P):&amp;#039;&amp;#039;&amp;#039; To randomly permute an array &amp;lt;math&amp;gt;A[1..n]&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\begin{align}&lt;br /&gt;
&amp;amp;\text{For } i = n \text{ down to } 2: \\&lt;br /&gt;
&amp;amp;\qquad j \leftarrow \text{random integer in } [1, i] \\&lt;br /&gt;
&amp;amp;\qquad \text{swap } A[i] \leftrightarrow A[j]&lt;br /&gt;
\end{align}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This produces each of the &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; permutations with equal probability. It runs in &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; time. Knuth attributes this method to Fisher and Yates (1938) and credits Durstenfeld (1964) with the computer implementation.&lt;br /&gt;
&lt;br /&gt;
A common pitfall is using &amp;lt;math&amp;gt;j \leftarrow&amp;lt;/math&amp;gt; random in &amp;lt;math&amp;gt;[1, n]&amp;lt;/math&amp;gt; (instead of &amp;lt;math&amp;gt;[1, i]&amp;lt;/math&amp;gt;), which yields a non-uniform distribution with &amp;lt;math&amp;gt;n^n&amp;lt;/math&amp;gt; equally likely outcomes — not a uniform permutation.&lt;br /&gt;
&lt;br /&gt;
===What Is a Random Sequence?===&lt;br /&gt;
&lt;br /&gt;
Knuth explores this deep philosophical question (Section 3.5, marked with an asterisk as optional). Several definitions have been proposed:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Von Mises–Wald–Church definition:&amp;#039;&amp;#039;&amp;#039; A sequence is random if it satisfies all &amp;quot;place selection&amp;quot; rules (computable subsequence selection). Church formulated this in terms of computable functions, making it more rigorous.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Martin-Löf definition (1966):&amp;#039;&amp;#039;&amp;#039; A sequence is random if it does not belong to any &amp;#039;&amp;#039;effectively null set&amp;#039;&amp;#039; — a set of measure zero that can be described by a computable sequence of statistical tests. This is the foundation of algorithmic randomness.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Kolmogorov complexity:&amp;#039;&amp;#039;&amp;#039; A finite sequence is random if its shortest description (program that generates it) is as long as the sequence itself. An infinite sequence is random if all its prefixes are incompressible.&lt;br /&gt;
&lt;br /&gt;
In practice, for computer applications, Knuth settles on a pragmatic definition: a sequence is &amp;quot;sufficiently random&amp;quot; if it passes a battery of statistical tests relevant to the intended application, and if knowledge of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; elements does not allow practical prediction of the &amp;lt;math&amp;gt;(n+1)&amp;lt;/math&amp;gt;st element.&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
&lt;br /&gt;
Knuth summarizes the main recommendations:&lt;br /&gt;
&lt;br /&gt;
* Use a linear congruential generator with a large modulus (at least &amp;lt;math&amp;gt;2^{30}&amp;lt;/math&amp;gt;).&lt;br /&gt;
* Choose the multiplier &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; to satisfy the spectral test in dimensions 2 through 6.&lt;br /&gt;
* For &amp;lt;math&amp;gt;c \neq 0&amp;lt;/math&amp;gt;, the increment need only be relatively prime to &amp;lt;math&amp;gt;m&amp;lt;/math&amp;gt;; &amp;lt;math&amp;gt;c = 1&amp;lt;/math&amp;gt; is convenient.&lt;br /&gt;
* Use the most significant bits of &amp;lt;math&amp;gt;X_n&amp;lt;/math&amp;gt;, not the least significant ones, when reducing to a smaller range.&lt;br /&gt;
* Subject the generator to multiple empirical tests before relying on it.&lt;br /&gt;
* For serious Monte Carlo work, consider combined generators or more modern methods with even longer periods.&lt;br /&gt;
&lt;br /&gt;
=Example Problems=&lt;br /&gt;
&lt;br /&gt;
==Spectral Test Calculation==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Problem:&amp;#039;&amp;#039;&amp;#039; Compute the two-dimensional spectral test value &amp;lt;math&amp;gt;\nu_2&amp;lt;/math&amp;gt; for the generator &amp;lt;math&amp;gt;X_{n+1} = (3 X_n + 0) \bmod 7&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Solution===&lt;br /&gt;
&lt;br /&gt;
The generator has &amp;lt;math&amp;gt;m = 7&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;a = 3&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;c = 0&amp;lt;/math&amp;gt;. In two dimensions, we seek:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\nu_2 = \min \left\{ \sqrt{x_1^2 + x_2^2} \;\middle|\; x_1 + 3 x_2 \equiv 0 \pmod{7}, \; (x_1, x_2) \neq (0,0) \right\}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The condition &amp;lt;math&amp;gt;x_1 \equiv -3 x_2 \pmod{7}&amp;lt;/math&amp;gt;. For &amp;lt;math&amp;gt;x_2 = 1&amp;lt;/math&amp;gt;: &amp;lt;math&amp;gt;x_1 \equiv 4&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;\sqrt{4^2 + 1^2} = \sqrt{17} \approx 4.12&amp;lt;/math&amp;gt;. For &amp;lt;math&amp;gt;x_2 = 2&amp;lt;/math&amp;gt;: &amp;lt;math&amp;gt;x_1 \equiv 1&amp;lt;/math&amp;gt;, giving &amp;lt;math&amp;gt;\sqrt{1^2 + 2^2} = \sqrt{5} \approx 2.24&amp;lt;/math&amp;gt;. Exhaustive checking confirms the minimum is &amp;lt;math&amp;gt;\sqrt{5}&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;\nu_2 = \sqrt{5}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The figure of merit threshold at &amp;lt;math&amp;gt;t=2&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;0.1 \cdot 7^{1/2} \approx 0.265&amp;lt;/math&amp;gt;. Since &amp;lt;math&amp;gt;\nu_2 = \sqrt{5} \approx 2.24 \gg 0.265&amp;lt;/math&amp;gt;, this generator passes the 2D spectral test (as expected for a full-period generator with prime modulus).&lt;br /&gt;
&lt;br /&gt;
==RANDU Failure==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Problem:&amp;#039;&amp;#039;&amp;#039; Show why RANDU (&amp;lt;math&amp;gt;X_{n+1} = 65539 X_n \bmod 2^{31}&amp;lt;/math&amp;gt;) fails the 3D spectral test.&lt;br /&gt;
&lt;br /&gt;
===Solution===&lt;br /&gt;
&lt;br /&gt;
For RANDU, &amp;lt;math&amp;gt;m = 2^{31}&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;a = 65539 = 2^{16} + 3&amp;lt;/math&amp;gt;. In 3 dimensions, all triples &amp;lt;math&amp;gt;(X_n, X_{n+1}, X_{n+2})&amp;lt;/math&amp;gt; satisfy:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
X_{n+2} = 6 X_{n+1} - 9 X_n \pmod{2^{31}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This follows from the identity &amp;lt;math&amp;gt;a^2 = 6a - 9 \pmod{m}&amp;lt;/math&amp;gt; (since &amp;lt;math&amp;gt;(2^{16}+3)^2 = 2^{32} + 6 \cdot 2^{16} + 9 \equiv 6a - 9 \pmod{2^{31}}&amp;lt;/math&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Thus all points lie on the plane &amp;lt;math&amp;gt;x_1 - 6 x_2 + 9 x_3 \equiv 0&amp;lt;/math&amp;gt;, and by symmetry on just 15 widely spaced planes in 3-space — a catastrophic failure of randomness.&lt;br /&gt;
&lt;br /&gt;
=Related=&lt;br /&gt;
&lt;br /&gt;
[[AOCP/Positional Number Systems]] — the next section in Volume 2, covering the foundations of computer arithmetic.&lt;br /&gt;
&lt;br /&gt;
[[AOCP/Floating Point Arithmetic]] — the section on floating-point representation and rounding errors.&lt;br /&gt;
&lt;br /&gt;
[[AOCP/Generating Permutations and Tuples]] — covers Algorithm P (Fisher–Yates shuffle) and other combinatorial generation techniques from Volume 4.&lt;br /&gt;
&lt;br /&gt;
=Flags=&lt;br /&gt;
&lt;br /&gt;
{{AOCPFlag}}&lt;br /&gt;
&lt;br /&gt;
{{AlgorithmsFlag}}&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>