TAOCP Vol 2: Seminumerical Algorithms · Week 64

TAOCP 3.1 Introduction

Updated

About 3 hours23 exercises, rated 10 to M48

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.

Flashcards 14 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §3.1 Introduction

    Implement the middle-square method and watch it fall into a short cycle.

  2. Work the exercises (8 picked)

    Start with Ex 1, Ex 3, Ex 5; log what you solve below.

  3. 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.

    Discuss

  • Exercise 310

    One step of the middle-square method by hand.

    Discuss

  • Exercise 515

    Argue why any generator of this kind must eventually repeat, before computing anything.

    Discuss

  • 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.

    Discuss

  • Exercise 7M21▶ Recommended

    Brent's cycle detection with powers of two. Implement it next to exercise 6 and compare function evaluations.

    Discuss

  • Exercise 823

    Exhaustively analyze two-digit middle-square. A small program makes the fate of every seed visible.

    Discuss

  • Exercise 11M26▶ Recommended

    Probability that a random mapping with a random seed has no repeats for a while. Birthday-paradox thinking.

    Discuss

  • 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.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign 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 posts

Questions 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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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.

Spot a mistake? Report it

Back to week 64