TL;DR
A random variable is a rule that turns each outcome of a random experiment into a number. Once outcomes are numbers, we can ask how likely each value is, what a typical value looks like, and how two quantities relate. Most of statistics is about random variables.
Where you'll see it: in the number of heads in ten coin flips, in the time a web request takes, in the noise added to a sensor reading, and in the hidden state of a model.
Intuition
Naming an outcome with a number
Flip two coins. The experiment has four outcomes, HH, HT, TH and TT, and none of them is a number. If you only care how many heads came up, you can attach a number to each outcome: 2, 1, 1, 0. That attachment is the random variable. It is a measuring device applied to a random outcome, so the value you see is random because the outcome is.
Three pieces are involved. The sample space is the set of all outcomes. The outcome itself, written , is what actually happens on one run. The random variable is a fixed rule that takes and returns a number . The randomness lives in . The rule is not random.
flowchart LR W[Outcome omega, random] --> X[Random variable X, a fixed rule] X --> V[Number such as 0, 1 or 2]
Why bother? Numbers can be added, averaged, compared and plotted. Outcomes like "heads then tails" cannot. Turning outcomes into numbers is what lets us use arithmetic and the tools built on top of it, such as the expected value and the variance.
Discrete and continuous
A random variable is discrete if it takes values from a list you could count, like 0, 1, 2 heads. It is continuous if it can take any value in an interval, like the time a request takes. The two cases use different tools, and the difference matters most when we come to probabilities of single values.
The math
Definition
A random variable is a function that assigns a real number to each outcome in the sample space . It is defined on a probability space , so that events about have probabilities.
An event about is a set of outcomes. For example, the event " equals " is the set , the outcomes the rule maps to . Its probability is written . Writing is shorthand for "the probability of the outcomes that sends to ".
Distributions for discrete variables
For a discrete , the probability mass function (PMF) is
Each value is non-negative, and the values add up to 1: , where the sum runs over every value that can take. The PMF is the full description of a discrete random variable. It tells you which values are possible and how likely each one is.
The cumulative distribution function
The cumulative distribution function (CDF) works for discrete and continuous variables alike:
It never decreases as grows, it approaches 0 on the far left and 1 on the far right, and the probability of an interval follows from it: .
Continuous variables
For a continuous , a single point has zero probability: for every . A point has no width, so it cannot hold any of the probability. Probabilities come from intervals instead, through a density , which is the subject of the probability density function:
The CDF is then . Here itself is not a probability and can be larger than 1. Only areas under are probabilities.
Functions of a random variable
Apply a rule to and you get a new random variable , built on the same outcomes. If is the number of heads in two coin flips and , then takes the values 1, 3 and 5 with probabilities , and . The probability of is the probability of , because .
When two random variables are considered together, their joint behaviour is described by joint probabilities such as . Whether they are related at all is the question of statistical independence.
Worked examples
Flip a fair coin twice. The sample space is , and each outcome has probability . Let be the number of heads.
- Map each outcome to a number: , , , .
- Count the outcomes that give each value. has one outcome, has two, has one.
- So , , and .
- Check: , as it must.
- The probability of at least one head is .
Interpretation: the random variable turns four equally likely sequences into three numbers with unequal weights. The middle value is the most likely because two of the four sequences produce it.
Roll two fair dice and let be the sum of the faces. The sample space has 36 equally likely ordered pairs.
- only for , so .
- for , six pairs, so .
- covers the sums 2, 3 and 4, which have pairs between them. So .
- The CDF at 4 is , and requires the sums 5, 6 and 7, which contribute pairs, a probability of .
Interpretation: is discrete, so probabilities attach to single values, and a table of 11 numbers describes it completely.
A request waits seconds, where is uniform between 0 and 10. Its density is on and 0 elsewhere, so its CDF is for .
- .
- .
- , because a single value has no width. The chance of landing exactly on 5.000000 seconds is zero, yet still lands somewhere every time.
Interpretation: questions about continuous variables are always about intervals. A request finishes within 2.5 seconds one time in four.
- CDF of T, uniform on [0, 10]
Tradeoffs and failure modes
Treating a random variable as a single fixed number is the most common mistake. The rule is fixed, but its output changes from run to run, so the right question is always about the distribution, not one value.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Using a density value as if it were a probability | For continuous , can exceed 1 and is not a chance | Compute the area under over an interval |
| Asking for on a continuous variable | The answer is always 0, which hides the real question | Ask about an interval, or use the CDF |
| Treating a count as continuous | A count jumps in whole steps, so a smooth density misstates it | Use a PMF for counts |
| Calling a fixed but unknown quantity random | Mixes up uncertainty about a fixed value with randomness in a process | Say explicitly what is random: the data, a parameter, or a hidden state |
Random variables also hide dependence. Two variables with the same distribution can still be tied together. The distribution of each alone does not reveal that, so check the joint behaviour before combining them.
History
Early probability was about games of chance. In 1654, Blaise Pascal and Pierre de Fermat exchanged letters about how to divide the stakes of an interrupted game. They reasoned about outcomes and their weights, but they had no general notion of a quantity that takes values.
Through the 1700s, Jacob Bernoulli, Abraham de Moivre and Pierre-Simon Laplace worked out the distributions of counts and sums, such as the number of successes in many trials. These were the first distributions of quantities that we would now call random variables, though the term had not been fixed yet.
The modern definition came with Andrey Kolmogorov's 1933 axiomatisation of probability, which described a random variable as a function on a probability space. That formulation let one set of tools, sums, averages and integrals, handle dice, measurements and noise in a single framework. It is why the idea caught on and why nearly every later statistics text starts here.
Check yourself
QuestionIn your own words, what is a random variable? Is it random?Show answer
A random variable is a rule that assigns a number to each outcome of a random experiment. The rule itself is fixed and not random. What is random is the outcome, and so the value the rule produces.
QuestionYou roll two fair dice and let be the larger of the two faces. What is ?Show answer
means both dice show 1, 2 or 3. That is of the 36 equally likely pairs, so .
QuestionWhy is for a continuous wait time , even though always takes some value?Show answer
A single point has no width, so it covers none of the area under the density. Probability for a continuous variable comes from intervals, where the area is positive. The value 5 is not impossible, but the chance of landing exactly on it is zero.
QuestionA store's waiting time , in minutes, is uniform between 0 and 20. What is , and what does it tell a manager?Show answer
. About one customer in four waits more than 15 minutes, so a manager should plan staffing for that tail, not just for the average wait of 10 minutes.
Sources
- Random variable (Wikipedia): https://en.wikipedia.org/wiki/Random_variable
- Probability distribution (Wikipedia): https://en.wikipedia.org/wiki/Probability_distribution
- Probability axioms (Wikipedia): https://en.wikipedia.org/wiki/Probability_axioms