#separator:tab
#html:true
#notetype:Basic
#tags column:3
#deck:cracked.guide::TAOCP Vol 2::3.4.2 Random Sampling and Shuffling
#columns:Front	Back	Tags
TAOCP §3.4.2 Random Sampling and Shuffling: what does it cover?	Picking n records at random from N, both when N is known (selection sampling) and when it is not known in advance (reservoir sampling), and shuffling an array uniformly.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::about
TAOCP §3.4.2: why does it matter?	Reservoir sampling and the Fisher-Yates shuffle are must-know algorithms. Getting the shuffle subtly wrong is a famous bug in real products.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::why
Fisher-Yates shuffle: what is it?	The Fisher-Yates shuffle produces a uniformly random permutation of an array in one pass by swapping each position with a randomly chosen position at or before it.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::definition
Fisher-Yates shuffle: Why is it called the Knuth shuffle?	Fisher and Yates published a pencil-and-paper version in 1938; Durstenfeld gave the computer version in 1964; Knuth popularized it in TAOCP.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::definition
TAOCP §3.4.2, a common trap: “Swapping with a random index from the whole array instead of the remaining part.” What's the catch?	That gives a biased shuffle.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::trap
TAOCP §3.4.2: what traps should you watch for?	<ul><li>Off-by-one on the range of the random index.</li></ul>	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::traps
TAOCP §3.4.2, exercise 3 (rated 22, recommended): what is it asking?	Resolve the apparent paradox that the selection probability changes but the sample is still fair.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 9 (rated 12): what is it asking?	Run reservoir sampling by hand with given random numbers.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 10 (rated 15): what is it asking?	Reservoir sampling without the reservoir file when the sample fits in memory. The version most people use.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 7 (rated M25): what is it asking?	Prove that every subset is equally likely under selection sampling.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 16 (rated M25, recommended): what is it asking?	Sample with hashing in O(n) space and time and output it sorted.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 17 (rated M22): what is it asking?	Prove Floyd's elegant sampling algorithm correct.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 15 (rated 30, recommended): what is it asking?	Get a random permutation of a random combination cheaply by stopping the shuffle early.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2, exercise 8 (rated M39, recommended): what is it asking?	Vitter's method of skipping records instead of testing each one. The stretch.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercise
TAOCP §3.4.2: how many exercises, and how are they rated?	17 exercises, rated M12 to M39.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::exercises
TAOCP §3.4.2: what should you be comfortable with first?	<ul><li>3.4.1 Numerical Distributions</li></ul>	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::prereqs
TAOCP §3.4.2: how should you work through it?	Implement the shuffle and verify by running it a million times on a 3-element array and counting all 6 outcomes. Then implement the &quot;naive&quot; swap-with-any-position shuffle and see its bias. Implement reservoir sampling over a stream read from stdin.	cracked.guide cracked.guide::phase::taocp-2 cracked.guide::section::3.4.2 cracked.guide::kind::approach
