Pseudo-Random Number Generators
Context: FIT2086_MOC Β· the engine under every simulation, Bootstrap and permutation test Β· R usage (set.seed, runif, rnorm) β R Simulation and Random Sampling
Quick Revision
- π― Objective: a deterministic recurrence whose output resembles iid uniforms β the seed fixes the whole sequence β simulations become exactly repeatable.
- β‘ Key Constraint: same seed + same generator βΉ identical sequence β the advantage (reproducibility) and the disadvantage (good seeding is crucial) are the same fact.
π Core
- Why βpseudoβ β a computer is fully deterministic βΉ no true randomness; a PRNGβs sequence is completely determined by the process and its state, yet is designed to have the statistical properties of independent random numbers.
- Recurrence β , = transition function; the initial = seed (usually the current time, sometimes a βtrueβ random source).
- Uniforms first β the primitive output is , on ; every other distribution is built from uniforms.
- Modern generator β SIMD-oriented Fast Mersenne Twister: fast, period up to before repeating; Rβs default generator is the Mersenne Twister.
- Other distributions β inverse-CDF procedure Β· MetropolisβHastings Β· acceptβreject Β· adaptive rejection sampling Β· slice sampling (named only) β in practice call
rnorm(),rpois(),rexp(). - LCG (teaching example) β β the current value alone determines the next βΉ the first repeated value starts a cycle.
π Exam Execution Trace
LCG with seed , , , :
- Output β βΉ a repeating cycle of length β a toy period; a usable generator needs an astronomically long one.
- R reproducibility β
set.seed(5); runif(n = 10, min = 0, max = 1)run twice prints the same ten numbers; a different seed gives a different sequence.
β οΈ Common Mistakes
- π‘ βRandomβ output called truly random β it is deterministic given the seed; that is exactly what makes a simulation repeatable.
- π‘ Re-seeding only once β one
set.seed()fixes the starting state; the generator advances with every draw, so a later block differs unless re-seeded (R Simulation and Random Sampling).
π§ Active Recall
Why is "same seed βΉ same sequence" both an advantage and a disadvantage?
Answer
- Short answer: advantage β any simulation can be re-run exactly (debugging, marking, reporting); disadvantage β a poor or reused seed reproduces the same βrandomβ numbers, so seeding must be done well.
- Why: Deterministic recurrence β makes the whole stream a function of .
An LCG uses modulus . What is the longest sequence it can produce before repeating?
Answer
- Short answer: at most distinct values β then it must cycle (the , seed example cycles after just ).
- Why: Finite state β depends only on , so the first repeated value replays the same continuation forever.