Random Numbers 🎲

"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin." — John von Neumann

Lecture 2-7

1. Old-Fashioned Randomness 📜

Slides 1-5

The Pencil Method

A pencil with sides A, B, C, D. Useful for exams, but not for scientific computing! It lacks speed and precision.

Random Number Tables

Before computers, people used books full of random digits. These tables taught us two fundamental properties of pseudo-randomness:

  • You need a seed to start.
  • It relies on recursive operations.

True vs Pseudo Generators

True Random:

Measures physical phenomena (thermal noise, photoelectric effect). Essential for cryptography.


Pseudo Random:

Computational algorithms. Deterministic sequences determined by a seed. Good enough for scientific work.

2. Linear Congruential Generator (LCG)

Slides 7-11

A classical algorithm. Simple, commonly used, but potentially dangerous.

$$ R_{next} = (a \cdot R_{current} + c) \mod M $$

Where $a, c$ are scaling constants and $M$ is the modulus. The period is at most $M$.

Example: A Bad LCG ($M=100$)

Watch how quickly the sequence repeats.

Example: Park-Miller RNG (Good)

Uses $a=16807, c=0, M=2^{31}-1$. The period is almost $2^{31}-1$.

⚠️ The RANDU Disaster

IBM used $a=65539, M=2^{31}$ in the 1960s. It produces numbers that fall into just 15 planes in 3D space!

3. Bitwise Generators 💻

Slides 19-27

XORSHIFT Algorithm

$x \leftarrow x \text{ xor } (x \gg a_1)$
$x \leftarrow x \text{ xor } (x \ll a_2)$
$x \leftarrow x \text{ xor } (x \gg a_3)$

Multiply With Carry (MWC)

Uses 64-bit logic.
$(x \& \text{mask}) \cdot a + (x \gg 32)$

Combined Generator (The Beast)

Combining XORSHIFT + MWC yields a massive period of $\approx 10^{37}$.

4. Non-Uniform Distributions 📊

Slides 30-50

❌ The Wrong Way

Just plugging random numbers into $f(x)$ does not produce distribution $f(x)$.

✅ Method 1: Hit-or-Miss (Rejection Sampling)

Generate random $(x, y)$. Keep $x$ only if $y < f(x)$. Simple but inefficient.

✅ Method 2: Inverse Transform

Solve $x = W^{-1}(r)$.
Example: Exponential Decay $f(x) = e^{-x}$. Cumulative $W(x) = 1 - e^{-x}$. Inverse: $x = -\ln(1-r)$.

Gaussian: Box-Muller

Transforming uniform coords to polar/Gaussian.

Gaussian: CLT Summation

Sum of 12 uniform randoms $\to$ Gaussian.

5. Monte Carlo Integration 🎯

Slides 52-55
$$ I \approx (b-a) \langle f \rangle = \frac{V}{N} \sum f(x_i) $$

6D Integral Example

Integrating $\sum \sin$ terms over a 6D hypercube. Grid methods fail here!

👐 Hands-On Session

Practice 1: Step Function

Generate distribution for:

f(x) = 1 (0 < x <=1)
f(x) = 2 (1 < x <=2)
...
f(x) = 9 (8 < x <=9)

Practice 2: Complex Model

Generate 10k events for the Higgs fit model:

$$ f(x) = \text{Gaussian} + ax^2 + bx + c $$