What a random variable really is
Today let's understand what a random variable really is, with an example you can picture.
Kavya runs a small sourdough stall. She bakes exactly 20 loaves every morning, and by closing time some of them have sold. Her logbook keeps the last 20 mornings, and the count of loaves sold moves around: 16 one day, 12 the next, 18 on a particularly good one.
Here are all 20 of those mornings, one bar per day.
Twenty mornings, and the number keeps landing somewhere different. So before anything else, there is one question worth answering: what kind of number is "loaves sold", exactly? That is what this lesson is about.
A random variable is a rule, not a number
Each of those bars is a number: 16, or 12, or 18. But none of those numbers is really what we want to understand. What we want to understand is the thing that produces that number, morning after morning.
Call it X, the number of loaves Kavya sells today. X is not 16, and it is not 12. X is the rule: count how many of the 20 baked loaves find a buyer by closing time. That rule is exactly the same every single day. What changes is the outcome of applying it, which morning you happen to be counting.
Discrete random variables: counting the loaves sold
Go back to X, the number of loaves Kavya sells today. However busy or quiet the morning, X can only land on one of 21 possible values: 0, 1, 2, 3, and so on, up to 20. It cannot read 14.5, because half a loaf sold does not happen, and it cannot read 23, because she only baked 20.
Write out every value X could possibly take, and you get a clean, countable list: 0, 1, 2, 3, all the way to 20. Nothing fits between 14 and 15. The very next value up from 14 is 15, full stop.
A random variable whose possible values form a list you could count off one at a time, even if the list is long, is called a discrete random variable. Loaves sold is one.
Continuous random variables: the weight of a loaf
Now look at a different number from the same stall: the weight of one loaf. Kavya's loaves weigh close to 900 grams each. Three real weighings from her kitchen scale came back 897.4 grams, 900.1 grams and 902.6 grams.
Unlike loaves sold, this number is not stuck on a short, countable list. Between 900.1 grams and 902.6 grams sits 901 grams. Between 900.1 and 901 sits 900.5. Between 900.1 and 900.5 sits 900.3, and so on forever. However finely you slice the gap between any two weights, another weight always fits inside it.
A random variable whose possible values cannot be listed one at a time, because there is always another value squeezed in between any two, is called a continuous random variable. A loaf's weight is one. Loaves sold is not: it is discrete.
Quick check: discrete or continuous
Kavya starts logging one more thing: how many minutes her oven stays on each morning. Three mornings read 42.0, 42.3 and 42.31.
Is "minutes the oven stays on" discrete or continuous?
A population figure versus a sample statistic
Kavya has run this stall for about three years now, somewhere around 900 mornings. Across all of them, 75% of what she bakes has sold by closing time. That 75% was not calculated from last Tuesday, or from any one week. It describes the stall's long-run behaviour, fixed, and the same number whether you ask about this year or last year. Call a number like that a population figure, a property of the process itself, here the 75% sell-through rate, rather than of any one batch of days you happen to look at.
Her logbook of 20 real mornings is a sample, a small slice drawn from that long run. In that sample, the average loaves sold works out to 14.65, not exactly 20 times 0.75, which is 15. Close, but not exact. Nothing has gone wrong. That is what a sample of 20 mornings drawn from a 75% long-run rate looks like. The population figure is 15, fixed. The sample average, 14.65, is just what these particular 20 mornings happened to show.
That distinction, between the fixed figure behind the process and the number one sample of days hands you, is exactly what expectation and variance are built to pin down.
Expectation as a long-run average
So the population sell-through rate, 75%, is fixed. But what does that actually predict about X, the number of loaves sold on any one morning?
Let's find out by simulating. rbinom() draws from a binomial distribution: give it a number of independent chances, here 20 loaves, each either selling or not, and a probability for each one, here 0.75, and it returns how many of them came out a sale. One call to rbinom(1, 20, 0.75) is one simulated morning. Repeat that 2,000 times with replicate(), and you get 2,000 simulated mornings of loaves sold.
Across those 2,000 simulated mornings, the average lands at 15.058, not 14.65. That is because 2,000 mornings pin down the long-run behaviour far more tightly than Kavya's 20 real ones ever could. Run even more simulated mornings and that average keeps settling closer to one fixed number.
That fixed number has a name: the expectation of X, written E[X]. It is the average X would settle to if the rule played out forever, not the average of any one batch of days. For a count built from n independent chances at probability p each, like 20 loaves each selling independently with probability 0.75, there is a shortcut that gives E[X] directly, with no simulation needed.
n times p, 20 times 0.75, lands at exactly 15. That matches the 2,000-morning simulation almost exactly, and it is no coincidence. The shortcut and the simulation are measuring the same fixed number from two different directions.
Press the buttons below and watch pure chance pile up, one simulated morning at a time, at Kavya's usual 75% rate.
Every bar above is a count of simulated mornings that landed on that many loaves sold out of 20. Press "Run 100" or "Run 1,000" a few times and watch where most of the bars pile up: it keeps settling in around 15, the same number the shortcut gave. The orange bars mark mornings that reached 18 or more loaves sold, a particularly good morning by Kavya's usual standard. Keep running games and you will see just how rare those are next to the pile sitting around 15.
Seeing the spread: variance in the simulated days
E[X] = 15 tells you where loaves sold is centred. It says nothing about how far a typical morning strays from that centre, and that is exactly what variance measures.
sim_days still holds the 2,000 mornings you just simulated. Tabulate them, and you can see the whole shape of where they landed.
Here is that same tally, bar by bar.
Most of the 2,000 mornings pile up around 14, 15 and 16, with far fewer out at 9 or 20. That shape, tall in the middle and thin at the edges, is what 2,000 mornings drawn at a 75% rate actually looks like. Kavya's own 20 real mornings, averaging 14.65, are a small and much noisier slice of exactly this same pile.
Variance measures the typical spread of that pile, as the average squared distance of every value from the centre, 15. Squaring matters because a morning 3 loaves below 15 and a morning 3 loaves above 15 should count as an equally sized miss, and squaring turns both into the same positive number instead of letting them cancel out.
That is the variance of the 2,000 simulated mornings, computed by hand: 3.913. Exactly as with expectation, a shortcut gives the same number with no simulation at all. For a count built from n independent chances at probability p each, the variance is n times p times (1 minus p).
3.75, matching the by-hand 3.913 almost exactly. The small gap is only because 2,000 mornings is a large sample, not an infinite one. Take the square root of either number and you get a typical swing of about 1.94 loaves around 15, in the same units as the loaves themselves, which is usually the easier number to picture.
So a count around 13 to 17 loaves on any given morning is unremarkable: it sits well within one typical swing of 15. That variance has a name too, written Var(X). It is the second fixed number the rule carries, Var(X) = 3.75, alongside E[X] = 15.
Closing quiz: the rule's number versus one sample's number
Three numbers have shown up for how many loaves Kavya sells: 14.65, the average of her own 20 real mornings; 15.058, the average of the 2,000 simulated mornings; and 15, which is 20 times 0.75. Which one is E[X], fixed by the rule itself, no matter which days or how many mornings get sampled?
Your turn: expectation and variance for a new rate
Kavya is testing a slower weekend recipe. On weekends, historically only 60% of the 20 loaves she bakes sell by closing time, instead of the usual 75%.
Repeat the same two moves from the last two steps for this new rate: simulate 2,000 such weekend mornings, then compute the mean and the average squared distance from the centre the rule predicts.
Show answer
# Simulate 2,000 weekend mornings at a 60% sell-through rate
set.seed(7)
weekend_days <- replicate(2000, rbinom(1, 20, 0.6))
mean(weekend_days)
#> [1] 11.9845
mean((weekend_days - 12)^2)
#> [1] 4.712511.9845 sits close to 12, which is 20 times 0.6. And 4.7125 sits close to 4.8, which is 20 times 0.6 times 0.4. Same two moves as before, a different rate, and the same match between simulation and the closed-form shortcut.
References
A few authoritative places to take this further:
- Wasserman, L., All of Statistics (Springer), Chapter 2, Random Variables - a compact, rigorous first pass at exactly the definitions taught here.
- Casella, G. and Berger, R., Statistical Inference (2nd ed., Chapman and Hall/CRC), Chapter 2, Transformations and Expectations - the fuller treatment of expectation and variance, including why the shortcuts work.
- NIST/SEMATECH e-Handbook of Statistical Methods, section 1.3.6.1, What is a Probability Distribution - a free reference page on discrete and continuous probability functions.
- Khan Academy, Random Variables (Statistics and Probability library) - short videos and practice problems on the same discrete versus continuous split.
Why E[X] describes the rule, not the dataset
Through this whole lesson, two things never changed: the batch size, 20 loaves, and the sell-through rate, 75%. Because those two never moved, E[X] = 15 and Var(X) = 3.75 never moved either. What changed, every time, was which sample happened to land in front of you: 14.65 on Kavya's own 20 real mornings, 15.058 on the first run of 2,000 simulated ones, and something slightly different again had that simulation run on another seed.
Neither 14.65 nor 15.058 is wrong. They are both just samples, drawn from a rule whose true centre and spread never move. That is what it means to say E[X] and Var(X) describe the rule itself, and not any one dataset, including Kavya's own.
Next, we will meet four distributions, the normal, the t, the chi-squared and the F, built from exactly this same idea of a rule with a fixed centre and spread, and see what each one is actually the distribution of.