TAOCP Vol 2: Seminumerical Algorithms · Week 64
TAOCP 3.1 Introduction
Updated
Summary
TAOCP 3.1 Introduction: What "random numbers" means for a computer, why truly random sources are awkward, and a tour of early attempts at generating pseudorandom numbers, including the middle-square method and why many ad hoc generators fail badly. Budget about 3 hours; do exercises 1, 3 and 5.
This week's steps
3 stepsRead §3.1 Introduction
Implement the middle-square method and watch it fall into a short cycle.
Start with Ex 1, Ex 3, Ex 5; 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 23 exercises, rated 10 to M48, and Knuth marks 5 of them with the triangle for recommended. Short tracing questions about the middle-square method sit next to a run of M exercises on random mappings. Do the quick ones, then spend real time on the cycle-detection pair (6 and 7): those algorithms show up in real code.
Worth your time
Exercise 120▶ Recommended
Judge several physical ways of getting a random digit. Sharpens what you actually mean by random.
Exercise 515
Argue why any generator of this kind must eventually repeat, before computing anything.
Exercise 6M21
Show that a sequence X(n+1) = f(X(n)) is eventually periodic, and find its cycle using almost no memory. This is Floyd's tortoise-and-hare; implement it.
Exercise 7M21▶ Recommended
Brent's cycle detection with powers of two. Implement it next to exercise 6 and compare function evaluations.
Exercise 823
Exhaustively analyze two-digit middle-square. A small program makes the fate of every seed visible.
Exercise 11M26▶ Recommended
Probability that a random mapping with a random seed has no repeats for a while. Birthday-paradox thinking.
Exercise 12M31▶ Recommended
Average cycle length and tail length for a random mapping. The stretch; ties into rho-style factoring later in 4.5.4.
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 120No 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 310No 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 515No 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 6M21No 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 7M21No 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 823No 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 11M26No 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 12M31No 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
Simulations, games, load testing, hashing and cryptography all depend on random numbers. This section is a warning against inventing your own generator.
Before you start
- Vol 1 chapter 1 math
How to approach it
Implement the middle-square method and watch it fall into a short cycle. That one experiment teaches the whole section. Read Knuth's story of his own "super-random" generator failing; it is a great lesson in humility.
Common traps
- Believing that complicated means random. The section's whole point is that it does not.