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
| # | Topic | Rating | Status |
|---|---|---|---|
| 1 | Period is at most ; exhibit a full-period generator | 10 | ⬜ |
| 2 | ▶ Hull–Dobell "if" direction for | M23 | ⬜ |
| 3 | Cycle structure of the generator | 10 | ⬜ |
| 4 | Chi-square: compute and decide | 20 | ⬜ |
| 5 | ▶ Reservoir sampling keeps each item with probability | 22 | ⬜ |
| 6 | Expected replacements in reservoir sampling | M25 | ⬜ |
Problems
1. Period is at most ; exhibit a full-period generator (rating 10 · cf. 3.2.1.2–1)
Problem. Show that the linear congruential sequence has period at most , from any seed. Then exhibit values giving period exactly for .
Hint. Each lies in , and is a function of alone. What must happen once you have generated values?
Answer sketch. There are only possible values, so among
( of them) two must coincide, say with
. Because the next value depends only on the current one, the sequence
repeats from there with period dividing . Hence the period is at
most . For , Hull–Dobell (Problem 2) gives full period whenever
and is odd — e.g. period(5, 3, 16, 0) == 16. By
contrast period(3, 1, 16, 0) < 16 () and
period(5, 2, 16, 0) < 16 ( even).
2. ▶ Hull–Dobell "if" direction for (rating M23 · cf. 3.2.1.2–4)
Problem. Theorem 3.2.1.2A says the LCG has full period iff (i) ; (ii) for every prime ; and (iii) if . Prove the if direction for the common case with : if and is odd, the period is .
Hint. For the three conditions collapse — the only prime dividing is , and . Translate each condition into a statement about and modulo small powers of two.
Answer sketch. With , : condition (i) is exactly " is odd"; condition (ii) requires , i.e. odd; condition (iii) requires . Now already forces odd, so it implies (ii) and (iii); and odd is (i). Thus the hypotheses give all three conditions, and the theorem yields period exactly . (The full proof that the three conditions are sufficient — the harder "if" of the general theorem — is Knuth's; here we only reduce the case to it, which is the instructive step and matches what Stage 1 checks empirically.)
3. Cycle structure of the generator (rating 10 · cf. 3.2.1–2)
Problem. Consider . Describe its full cycle structure: which values lead into which cycles, and what periods occur? Does it have full period?
Hint. Is the map a bijection on ? Compare with . A bijection has no "tails."
Answer sketch. Since , multiplication by is invertible mod , so is a bijection — every state has a unique predecessor, hence there are no tails, only disjoint cycles. Tracing them: (period ), (period ), and (period ). The period from a seed is the length of its cycle, so this generator does not have full period — its multiplier fails Hull–Dobell condition (iii). (Starting from you see .)
4. Chi-square: compute and decide (rating 20 · cf. 3.3.1–8)
Problem. A die is rolled times with observed face counts for faces –. Under the fair-die hypothesis each face is expected times. Compute the chi-square statistic and decide, at the level, whether to reject fairness. (The point of the chi-square distribution with degrees of freedom is .)
Hint. with .
This is exactly what chi_square_uniform(&[16,8,9,6,12,9]) returns; reject if
exceeds the tabulated critical value.
Answer sketch. With every expected count ,
There are degrees of freedom. Since , is not in the upper tail, so we do not reject the fair-die hypothesis — the counts are consistent with a fair die. (A near would be too good, and a above would be grounds to suspect the die.)
5. ▶ Reservoir sampling keeps each item with probability (rating 22 · cf. 3.4.2–8)
Problem. Algorithm R (Stage 3, single-element reservoir) reads a stream of unknown length: it keeps , then for each replaces the stored sample by with probability . Prove that after processing all items, every is the retained sample with probability exactly .
Hint. Induct on the stream length. Assume the claim holds after items; what happens to those probabilities when the -th item arrives and is accepted with probability ?
Answer sketch. Induction on . Base : is kept with probability . Step: suppose after items each of is stored with probability . On item : becomes the sample with probability (correct). Each earlier remains stored iff it was stored and is rejected, i.e. with probability So all items are equally likely, each with probability .
6. Expected replacements in reservoir sampling (rating M25 · cf. 3.4.2–10)
Problem. Over a stream of items, how many times does the single-element reservoir of Problem 5 get replaced (the stored sample overwritten), on average?
Hint. Let be the indicator that item triggers a replacement. for . Use linearity of expectation — no independence needed.
Answer sketch. The stored sample is overwritten at step (for ) with probability , independently of the value stored. By linearity,
where is the -th harmonic number. So the count grows like : about replacements for , and for large — strikingly few, which is why reservoir sampling is cheap. (You met in Module 02; it is the recurring cost signature of "touch item with probability .")
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.