#separator:tab
#html:true
#notetype:Basic
#tags column:3
#deck:cracked.guide::TAOCP Vol 2::3.2.1 The Linear Congruential Method
#columns:Front	Back	Tags
TAOCP §3.2.1 The Linear Congruential Method: what does it cover?	The classic generator X_{n+1} = (aX_n + c) mod m. 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.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::about
TAOCP §3.2.1: why does it matter?	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.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::why
Linear congruential generator: what is it?	A linear congruential generator produces pseudorandom numbers by the recurrence X(n+1) = (a X(n) + c) mod m for a fixed multiplier a, increment c and modulus m.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::definition
Linear congruential generator: Should I use an LCG today?	For reproducible simulations or games it can be fine with well-tested parameters. Never for anything security related, and modern generators are usually a better default.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::definition
TAOCP §3.2.1, a common trap: “Using the low bits of a power-of-two LCG.” What's the catch?	They have tiny periods.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::trap
TAOCP §3.2.1, a common trap: “Assuming full period means good quality.” What's the catch?	It does not; the spectral test (3.3.4) is about that.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::trap
TAOCP §3.2.1, exercise 2 of §3.2.1 (rated M20, recommended): what is it asking?	If a and m are coprime the seed always comes back. A first taste of why multipliers must be units.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 6 of §3.2.1.1 (rated 20, recommended): what is it asking?	Swap addition for subtraction in the recurrence and decide whether anything really changes.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 9 of §3.2.1.1 (rated M25, recommended): what is it asking?	Compute a*x mod m in a language without double-length products, without overflow. The trick behind most portable LCG code.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 11 of §3.2.1.1 (rated M30): what is it asking?	Push exercise 9 further: evaluate a*x mod m for any multiplier using only small safe operations. The stretch.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 3 of §3.2.1.2 (rated 13): what is it asking?	Full-period condition for a power-of-ten modulus, specialized from the main theorem. Quick check that you can apply it.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 6 of §3.2.1.2 (rated 20, recommended): what is it asking?	Find every full-period multiplier for one specific modulus by factoring it.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 7 of §3.2.1.2 (rated M23, recommended): what is it asking?	Pin down the tail length and period of an arbitrary congruential sequence, not just the full-period case.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1, exercise 8 of §3.2.1.3 (rated M26, recommended): what is it asking?	Explain why a generator with low potency fails a simple triples test. The same failure that sank famous real-world generators.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercise
TAOCP §3.2.1: how many exercises, and how are they rated?	49 exercises, rated 10 to 40.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::exercises
TAOCP §3.2.1: what should you be comfortable with first?	<ul><li>1.2.4 Integer Functions and Elementary Number Theory</li><li>3.1 Introduction</li></ul>	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::prereqs
TAOCP §3.2.1: how should you work through 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.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.2.1 cracked.guide::kind::approach
