Skip to main content

Exercises — Module 04 (Random Numbers)

Self-contained problems on this module's material — the LCG period, the Hull–Dobell full-period test, the chi-square test, shuffling, and reservoir sampling. You can work every one without the books: each states the problem in full, gives a hint to peek at when stuck, and a worked answer sketch to check against after you try. Computational answers here are reproduced by the code you write in the lab (or a few lines at a REPL).

Ratings follow Knuth's scale (00–50; M = needs mathematics, ▶ = especially instructive). Where a problem mirrors a TAOCP exercise, its number is noted for readers who own Volume 2.

Tracking​

#TopicRatingStatus
1Period is at most mm; exhibit a full-period generator10⬜
2▶ Hull–Dobell "if" direction for m=2em = 2^eM23⬜
3Cycle structure of the 7,7,7 mod 107,7,7 \bmod 10 generator10⬜
4Chi-square: compute VV and decide20⬜
5▶ Reservoir sampling keeps each item with probability 1/n1/n22⬜
6Expected replacements in reservoir samplingM25⬜

Problems​

1. Period is at most mm; exhibit a full-period generator (rating 10 · cf. 3.2.1.2–1)​

Problem. Show that the linear congruential sequence Xk+1=(aXk+c) mod mX_{k+1} = (aX_k + c) \bmod m has period at most mm, from any seed. Then exhibit values a,ca, c giving period exactly mm for m=16m = 16.

Hint. Each XkX_k lies in {0,1,…,m−1}\{0, 1, \dots, m-1\}, and Xk+1X_{k+1} is a function of XkX_k alone. What must happen once you have generated m+1m+1 values?

Answer sketch. There are only mm possible values, so among X0,X1,…,XmX_0, X_1, \dots, X_m (m+1m+1 of them) two must coincide, say Xi=XjX_i = X_j with i<ji < j. Because the next value depends only on the current one, the sequence repeats from there with period dividing j−i≤mj - i \le m. Hence the period is at most mm. For m=16m = 16, Hull–Dobell (Problem 2) gives full period 1616 whenever a≡1(mod4)a \equiv 1 \pmod 4 and cc is odd — e.g. period(5, 3, 16, 0) == 16. By contrast period(3, 1, 16, 0) < 16 (3≡3(mod4)3 \equiv 3 \pmod 4) and period(5, 2, 16, 0) < 16 (cc even).

2. ▶ Hull–Dobell "if" direction for m=2em = 2^e (rating M23 · cf. 3.2.1.2–4)​

Problem. Theorem 3.2.1.2A says the LCG has full period mm iff (i) gcd⁡(c,m)=1\gcd(c, m) = 1; (ii) a≡1(modp)a \equiv 1 \pmod p for every prime p∣mp \mid m; and (iii) a≡1(mod4)a \equiv 1 \pmod 4 if 4∣m4 \mid m. Prove the if direction for the common case m=2em = 2^e with e≥2e \ge 2: if a≡1(mod4)a \equiv 1 \pmod 4 and cc is odd, the period is 2e2^e.

Hint. For m=2em = 2^e the three conditions collapse — the only prime dividing mm is 22, and 4∣m4 \mid m. Translate each condition into a statement about aa and cc modulo small powers of two.

Answer sketch. With m=2em = 2^e, e≥2e \ge 2: condition (i) gcd⁡(c,2e)=1\gcd(c, 2^e) = 1 is exactly "cc is odd"; condition (ii) requires a≡1(mod2)a \equiv 1 \pmod 2, i.e. aa odd; condition (iii) requires a≡1(mod4)a \equiv 1 \pmod 4. Now a≡1(mod4)a \equiv 1 \pmod 4 already forces aa odd, so it implies (ii) and (iii); and cc odd is (i). Thus the hypotheses give all three conditions, and the theorem yields period exactly 2e2^e. (The full proof that the three conditions are sufficient — the harder "if" of the general theorem — is Knuth's; here we only reduce the 2e2^e case to it, which is the instructive step and matches what Stage 1 checks empirically.)

3. Cycle structure of the 7,7,7 mod 107,7,7 \bmod 10 generator (rating 10 · cf. 3.2.1–2)​

Problem. Consider Xk+1=(7Xk+7) mod 10X_{k+1} = (7X_k + 7) \bmod 10. Describe its full cycle structure: which values lead into which cycles, and what periods occur? Does it have full period?

Hint. Is the map x↦(7x+7) mod 10x \mapsto (7x + 7) \bmod 10 a bijection on {0,…,9}\{0, \dots, 9\}? Compare gcd⁡(7,10)\gcd(7, 10) with 11. A bijection has no "tails."

Answer sketch. Since gcd⁡(7,10)=1\gcd(7, 10) = 1, multiplication by 77 is invertible mod 1010, so x↦7x+7x \mapsto 7x + 7 is a bijection — every state has a unique predecessor, hence there are no tails, only disjoint cycles. Tracing them: {0→7→6→9→0}\{0 \to 7 \to 6 \to 9 \to 0\} (period 44), {1→4→5→2→1}\{1 \to 4 \to 5 \to 2 \to 1\} (period 44), and {3→8→3}\{3 \to 8 \to 3\} (period 22). The period from a seed is the length of its cycle, so this generator does not have full period 1010 — its multiplier a=7≡3(mod4)a = 7 \equiv 3 \pmod 4 fails Hull–Dobell condition (iii). (Starting from X0=7X_0 = 7 you see 7,6,9,0,7,6,9,0,…7, 6, 9, 0, 7, 6, 9, 0, \dots.)

4. Chi-square: compute VV and decide (rating 20 · cf. 3.3.1–8)​

Problem. A die is rolled n=60n = 60 times with observed face counts Y=(16,8,9,6,12,9)Y = (16, 8, 9, 6, 12, 9) for faces 11–66. Under the fair-die hypothesis each face is expected n/6=10n/6 = 10 times. Compute the chi-square statistic VV and decide, at the 95%95\% level, whether to reject fairness. (The 95%95\% point of the chi-square distribution with 55 degrees of freedom is ≈11.07\approx 11.07.)

Hint. V=∑s=16(Ys−nps)2npsV = \sum_{s=1}^{6} \dfrac{(Y_s - np_s)^2}{np_s} with nps=10np_s = 10. This is exactly what chi_square_uniform(&[16,8,9,6,12,9]) returns; reject if VV exceeds the tabulated critical value.

Answer sketch. With every expected count 1010,

V=(6)2+(−2)2+(−1)2+(−4)2+(2)2+(−1)210=36+4+1+16+4+110=6210=6.2.V = \frac{(6)^2 + (-2)^2 + (-1)^2 + (-4)^2 + (2)^2 + (-1)^2}{10} = \frac{36 + 4 + 1 + 16 + 4 + 1}{10} = \frac{62}{10} = 6.2.

There are k−1=5k - 1 = 5 degrees of freedom. Since 6.2<11.076.2 < 11.07, VV is not in the upper 5%5\% tail, so we do not reject the fair-die hypothesis — the counts are consistent with a fair die. (A VV near 00 would be too good, and a VV above ≈11\approx 11 would be grounds to suspect the die.)

5. ▶ Reservoir sampling keeps each item with probability 1/n1/n (rating 22 · cf. 3.4.2–8)​

Problem. Algorithm R (Stage 3, single-element reservoir) reads a stream x1,x2,…,xnx_1, x_2, \dots, x_n of unknown length: it keeps x1x_1, then for each i≥2i \ge 2 replaces the stored sample by xix_i with probability 1/i1/i. Prove that after processing all nn items, every xjx_j is the retained sample with probability exactly 1/n1/n.

Hint. Induct on the stream length. Assume the claim holds after n−1n-1 items; what happens to those probabilities when the nn-th item arrives and is accepted with probability 1/n1/n?

Answer sketch. Induction on nn. Base n=1n = 1: x1x_1 is kept with probability 1=1/11 = 1/1. Step: suppose after n−1n-1 items each of x1,…,xn−1x_1, \dots, x_{n-1} is stored with probability 1n−1\tfrac{1}{n-1}. On item nn: xnx_n becomes the sample with probability 1n\tfrac{1}{n} (correct). Each earlier xjx_j remains stored iff it was stored and xnx_n is rejected, i.e. with probability 1n−1⋅(1−1n)=1n−1⋅n−1n=1n.\tfrac{1}{n-1}\cdot\big(1 - \tfrac1n\big) = \tfrac{1}{n-1}\cdot\tfrac{n-1}{n} = \tfrac1n. So all nn items are equally likely, each with probability 1/n1/n. ■\blacksquare

6. Expected replacements in reservoir sampling (rating M25 · cf. 3.4.2–10)​

Problem. Over a stream of nn items, how many times does the single-element reservoir of Problem 5 get replaced (the stored sample overwritten), on average?

Hint. Let RiR_i be the indicator that item ii triggers a replacement. E[Ri]=1/i\mathbb{E}[R_i] = 1/i for i≥2i \ge 2. Use linearity of expectation — no independence needed.

Answer sketch. The stored sample is overwritten at step ii (for i=2,…,ni = 2, \dots, n) with probability 1/i1/i, independently of the value stored. By linearity,

E[replacements]=∑i=2n1i=Hn−1,\mathbb{E}[\text{replacements}] = \sum_{i=2}^{n} \frac{1}{i} = H_n - 1,

where Hn=∑i=1n1/iH_n = \sum_{i=1}^{n} 1/i is the nn-th harmonic number. So the count grows like ln⁡n\ln n: about 1.931.93 replacements for n=10n = 10, and ≈ln⁡n+γ−1\approx \ln n + \gamma - 1 for large nn — strikingly few, which is why reservoir sampling is cheap. (You met HnH_n in Module 02; it is the recurring cost signature of "touch item ii with probability 1/i1/i.")


Your solutions​

Use this space to log your own work — a restated problem, your approach, and how it compared with the sketch above.

Exercise N (rating RR)​

Approach. Answer / proof. Compared with the sketch: what you did differently, what you missed.