TAOCP Vol 2: Seminumerical Algorithms · Week 65
TAOCP 3.2.1 The Linear Congruential Method
Updated
Summary
TAOCP 3.2.1 The Linear Congruential Method: The classic generator X_{n+1} = (aX_n + c) mod m. Budget about 8 hours; do exercises 3.2.1-2, 3.2.1.1-6 and 3.2.1.1-9.
Knuth covers how to choose the modulus, the exact conditions on a and c for the longest possible period, and the notion of potency that rules out multipliers that are technically fine but bad in practice.
See it
All visualsStep through at your own pace; the arrow keys work too.
Each dot is a pair (Xn, Xn+1)
Xn+1 = (a·Xn + c) mod m
(137·221 + 187) mod 256 = 0
Sequence
1361312146513295146221
Period 256 of a possible 256
Theorem A: a full period needs all three
- c and m share no factor (gcd 1): yes
- a − 1 is divisible by every prime of m (2): yes
- a − 1 is a multiple of 4, since 4 divides m: yes
X256 = 0 was seen before, so the period is 256, the full modulus, and every pair lies on a few parallel lines whose spacing the spectral test measures.
This week's steps
3 stepsRead §3.2.1 The Linear Congruential Method
Read 3.2.1.1 (modulus), 3.2.1.2 (multiplier) and 3.2.1.3 (potency).
Start with Ex 3.2.1-2, Ex 3.2.1.1-6, Ex 3.2.1.1-9; log what you solve below.
Write down one thing you'd explain to someone
A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.
Sign in to track this: tick each step off and it counts toward your week.
Exercises
This section has 49 exercises, rated 10 to 40, and Knuth marks 16 of them with the triangle for recommended. Counts cover 3.2.1 itself (exercises 1-5) and its subsections 3.2.1.1 Choice of modulus (14), 3.2.1.2 Choice of multiplier (22) and 3.2.1.3 Potency (8); picks are numbered by subsection. The multiplier exercises in 3.2.1.2 are the heart of the week; do those with a calculator or a few lines of code next to you.
Worth your time
Exercise 3.2.1-2M20▶ Recommended
If a and m are coprime the seed always comes back. A first taste of why multipliers must be units.
Exercise 3.2.1.1-620▶ Recommended
Swap addition for subtraction in the recurrence and decide whether anything really changes.
Exercise 3.2.1.1-9M25▶ Recommended
Compute a*x mod m in a language without double-length products, without overflow. The trick behind most portable LCG code.
Exercise 3.2.1.1-11M30
Push exercise 9 further: evaluate a*x mod m for any multiplier using only small safe operations. The stretch.
Exercise 3.2.1.2-313
Full-period condition for a power-of-ten modulus, specialized from the main theorem. Quick check that you can apply it.
Exercise 3.2.1.2-620▶ Recommended
Find every full-period multiplier for one specific modulus by factoring it.
Exercise 3.2.1.2-7M23▶ Recommended
Pin down the tail length and period of an arbitrary congruential sequence, not just the full-period case.
Exercise 3.2.1.3-8M26▶ Recommended
Explain why a generator with low potency fails a simple triples test. The same failure that sank famous real-world generators.
Your log
Sign in to log the exercises you work: number, rating, how far you got.
Your notes
Only youSign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.
Discussion
0 postsQuestions about the reading go here; each exercise has its own thread below. Hints over answers.
No posts yet. Ask a question or share a tip.
Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise threads
One thread per exercise. Open one to ask for a nudge or check your thinking.
Exercise 3.2.1-2M20No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.1-620No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.1-9M25No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.1-11M30No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.2-313No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.2-620No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.2-7M23No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3.2.1.3-8M26No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Reference
Why it matters
LCGs are still inside many standard libraries and games, and knowing their weaknesses (low bits with short periods, patterns in higher dimensions) saves you from subtle simulation bugs.
Before you start
- 1.2.4 Integer Functions and Elementary Number Theory
- 3.1 Introduction
How to approach it
Read 3.2.1.1 (modulus), 3.2.1.2 (multiplier) and 3.2.1.3 (potency). Write an LCG with a small modulus like 16 or 64 and print the full sequence to see the period theorem in action. Then print the lowest bit of a power-of-two-modulus LCG and watch it alternate.
Common traps
- Using the low bits of a power-of-two LCG. They have tiny periods.
- Assuming full period means good quality. It does not; the spectral test (3.3.4) is about that.