The Uniform Distribution
π² The simplest distribution: Uniform. Every value in a range is equally likely β like a perfectly fair, infinitely-sided die.
A random variable is a variable whose value is decided by a random process β the roll you haven't rolled yet. If x is drawn uniformly between a and b, we write it in shorthand:
x ~ U(a, b)
"x is distributed uniformly between a and b." The ~ means "is drawn from." Flat inside [a, b], zero outside.
In numpy, np.random.uniform(a, b, size=(n,)) draws n numbers between a and b. One quirk you'll lean on constantly: np.random.seed(0) freezes the randomness so you get the same "random" numbers every run. That sounds backwards, but it makes plots reproducible β your figure matches the solution exactly.
β οΈ Uniform does NOT mean evenly spaced. A fair process can still deal you clumps. "Random" and "smooth" are not the same thing β this trips up almost everyone.
Below you're sampling (x, y) points, each coordinate drawn from U(0, 1). Drag the slider and watch: with 10 points it looks lumpy and biased; only with hundreds does the square fill in evenly.
Every point is drawn from U(0, 1) β each spot in the box is equally likely. Yet with only a handful of points it looks clumpy, not smooth! Try dragging up to 500: the box only fills in evenly once you have lots of samples. That gap between "the rule" and "a small sample" is randomness.
Every point is equally likely to land anywhere β yet small samples look clumpy. Push it to 500 to see the uniformity finally appear.
π¦ Ilya says: This is your first gut-check on real data. When someone eyeballs 12 trials and declares a "cluster," remember the fog β small samples clump by chance.