Random Variables Turn Outcomes into Distributions
LESSON
Random Variables Turn Outcomes into Distributions
By the end of this lesson, you will be able to...
Map each outcome in a sample space to a numeric quantity that answers a specific question.
Build a small discrete distribution and read its probability mass and cumulative probabilities.
Explain why a continuous measurement uses intervals and density rather than probability at one exact value.
Idea in one sentence: A random variable is a deliberate measurement of an outcome, and its distribution describes how that measurement is spread across repeated runs.
Core Insight
In the previous lesson, a notification client had three complete histories:
- A: the first attempt was acknowledged.
- TA: the first attempt timed out and the retry was acknowledged.
- TT: both attempts timed out.
Those labels are useful for describing what happened. They are awkward for answering operational questions.
The team now wants to know:
How many client attempts does a typical request use?
The sample space already contains the answer, but it is hidden inside the labels. We need a rule that reads one outcome and returns one quantity:
| Outcome | Number of client attempts |
|---|---|
| A | 1 |
| TA | 2 |
| TT | 2 |
The rule is not a new experiment. It is a new view of the same experiment.
That view is a random variable.
The word “variable” can be misleading. In a program, a variable may be a box that changes when code assigns a new value. In probability, a random variable is better understood as a function: it takes one possible outcome and maps it to a number.
The mapping is chosen for a question. Different mappings over the same outcomes produce different distributions and different answers.
The Pattern We Want to Name
Start with a small phenomenon: requests sometimes finish on the first attempt, sometimes need a retry, and sometimes time out twice.
The sample space is:
Define a variable \(X\) for the number of client attempts:
Plain meaning:
\(X\) ignores the detailed acknowledgement history and keeps only the attempt count.
In this scenario:
The two histories TA and TT both map to 2 because both use two attempts, even though one eventually receives an acknowledgement and the other does not.
Technical name:
The function \(X\) is a random variable because uncertainty about the outcome makes the value of \(X\) uncertain before the request runs.
The function itself is fixed. The outcome is what varies from run to run.
This distinction is small but important:
- The sample space stores the possible histories.
- The random variable selects one measurement of each history.
- The distribution tells us how likely each measured value is.
A Tiny Discrete Example
From the previous lesson, use the toy probabilities:
To find the distribution of \(X\), group outcomes that map to the same value.
| Value of \(X\) | Outcomes that produce it | Probability |
|---|---|---|
| 1 | A | 0.80 |
| 2 | TA, TT | 0.16 + 0.04 = 0.20 |
Therefore:
The probabilities still add to 1:
This table is the probability mass function for \(X\). It places probability mass on the values that the variable can take.
The word “mass” is useful here. A count such as number of attempts jumps from one whole value to another. There is no meaningful value of 1.4 client attempts in this model.
A Worked Mapping: One Sample Space, Three Variables
The same request can produce several useful variables.
Variable 1: attempt count
This variable answers:
How much retry work does the client perform?
Its distribution is:
Variable 2: eventual acknowledgement
Define an indicator \(S\):
This variable answers:
Did the client eventually receive an acknowledgement?
Its distribution is:
An indicator is a useful variable with only two values, often 0 and 1. The numbers are labels for a yes/no property. They do not mean that success is “twice as much” as failure.
Variable 3: a coarse latency bucket
Suppose the client records the time until an acknowledgement or final timeout. We could create a bucketed variable \(B\):
- \(B=1\): under 100 milliseconds.
- \(B=2\): from 100 to 500 milliseconds.
- \(B=3\): over 500 milliseconds or final timeout.
This variable answers:
Which latency band did the client observe?
The same outcome can map to different values of \(B\) depending on the measured timing. A first-attempt acknowledgement can be fast or slow. The outcome label alone does not contain every quantity we may want.
Here is the complete reasoning path:
- Begin with the outcome history.
- Ask which operational question matters.
- Define a function that returns one quantity for every represented outcome.
- Group outcomes that produce the same value.
- Add their probabilities to obtain the variable’s distribution.
- Check whether the resulting values still preserve the distinction the decision needs.
The last step prevents a common modeling mistake. \(X\) is excellent for retry load, but \(X\) cannot tell us whether a two-attempt request succeeded. For that question, use \(S\) as well.
So far, the key move is not arithmetic. It is choosing a measurement that preserves the question we care about.
Cumulative Probabilities Answer Threshold Questions
Sometimes the team does not ask for one exact value. It asks for a threshold:
What is the probability that a request uses at most one attempt?
For \(X\), the answer is:
Another question is:
What is the probability that a request uses at most two attempts?
Every represented outcome uses at most two attempts:
The function
is the cumulative distribution function, or CDF.
For this variable:
| Threshold \(x\) | \(F_X(x)\) |
|---|---|
| less than 1 | 0.00 |
| 1 | 0.80 |
| between 1 and 2 | 0.80 |
| 2 or more | 1.00 |
The CDF is useful because real questions often have limits:
- At most two retries.
- Under 500 milliseconds.
- No more than three failures in a batch.
- Less than a given cost.
The CDF accumulates probability as the threshold moves from left to right. For a discrete variable, it looks like steps.
Counts and Measurements Behave Differently
Number of attempts is a discrete variable. It takes separated values: 1, 2, 3, and so on.
Latency is usually modeled as a continuous variable. In principle, a response could take 101.2 milliseconds, 101.21 milliseconds, or a value between any two measurements.
For a continuous variable, a useful probability question is about an interval:
where \(L\) is latency in milliseconds.
In an ideal continuous model, the probability of one exact point such as \(P(L=200)\) is 0. The probability lives in intervals. A density describes where probability is concentrated, but density itself is not the probability of one exact value. The probability of an interval is the area under the density across that interval.
You do not need calculus to use this idea:
- A high part of the density means values in that region are more concentrated.
- A wide interval can contain more probability than a narrow interval.
- The total area across all possible values is 1.
Real telemetry is recorded with limited resolution. A dashboard may show 200 milliseconds even when the underlying measurement was 199.6 or 200.4. The continuous model is an approximation that helps us reason about ranges; the stored data still arrives in buckets, rounded values, or samples.
This is another modeling trade-off:
- A discrete bucket is easy to count and explain, but it hides variation inside each bucket.
- A continuous model preserves a smoother view, but requires assumptions about how values are distributed and measured.
Use the representation that matches the question. If the alert is “p95 latency above 500 milliseconds,” a threshold and a CDF are more useful than pretending one exact latency value is fundamental.
What a Random Variable Does Not Do
A random variable does not automatically make the model more accurate.
If the original sample space omits late server processing, then a latency variable built from client observations still omits it. The function can be mathematically precise while measuring the wrong boundary.
A random variable also does not have to preserve every distinction in the outcome. That is often its purpose. \(X\) intentionally forgets whether the second attempt succeeded. It is a good variable for retry cost and a bad variable for delivery success.
The design question is:
Which information should this variable keep, and which information may it safely discard?
When one variable cannot answer two different questions, define a second variable. Do not force one number to carry incompatible meanings.
Edge Cases and Common Confusions
Confusion: The random variable is the same thing as the outcome
Why it is tempting:
The label TA and the value 2 both describe the same run.
Better model:
TA is an outcome in the sample space. \(X(TA)=2\) is the value assigned by one function. Another variable, such as \(S\), can assign a different value to the same outcome.
Confusion: A larger numeric label means a larger amount
Why it is tempting:
An indicator uses 0 and 1, and a category may use 1, 2, and 3.
Better model:
Numbers can be labels. Only some variables support arithmetic comparisons. For \(X\), 2 attempts is more work than 1 attempt. For a status code, 3 is not automatically “three times” status 1.
Confusion: A density value is a probability
Why it is tempting:
Both are written as numbers on a graph.
Better model:
A density describes concentration. Probability comes from the area over an interval. A tall, narrow region and a short, wide region can contain the same probability.
Confusion: More variables always make the model better
Why it is tempting:
Keeping every measurement feels safer.
Better model:
Extra variables increase collection cost, missing data, interpretation work, and opportunities for misleading relationships. Add a variable when it supports a question or exposes a known boundary.
Check Your Understanding
Check: The team wants to estimate retry load. Which variable is more direct: \(X\), the number of attempts, or \(S\), the success indicator?
Think first, then reveal.
Answer: \(X\). It maps each outcome to the amount of client attempt work. \(S\) answers eventual acknowledgement, not retry cost.
Check: Why does \(P(X=2)=0.20\) include both TA and TT?
Think first, then reveal.
Answer: Both outcomes map to the same value \(X=2\). The distribution of a variable groups outcomes by value and adds their probabilities.
Practice: Choose Variables for a Batch Job
Return to the batch-job outcomes from lesson 001:
- F: finishes before the deadline.
- L: finishes after the deadline.
- X: fails without finishing.
Define two variables:
- \(C\), completion status, where \(C=1\) means the job finishes at some point and \(C=0\) means it fails.
- \(D\), deadline status, where \(D=1\) means the job meets the deadline and \(D=0\) means it does not.
Using the historical counts of 150 F, 30 L, and 20 X from 200 runs, build the distribution of \(C\) and \(D\).
Then answer:
- What information does \(C\) preserve?
- What information does \(D\) preserve?
- What distinction do both variables discard?
- Which additional variable would you add if the team needed to estimate how late an L run was?
Model answer
The mappings are:
Therefore:
and:
\(C\) preserves eventual completion. \(D\) preserves whether the deadline was met. Both discard the difference between F and L unless that distinction is represented by another variable.
To estimate lateness, add a variable such as \(L_t\), the completion delay in minutes, with a defined value or missingness rule for failed runs. The rule must say what happens when a job has no completion time. That boundary is part of the variable design.
Connections
Lesson 001 chose outcomes and events. This lesson adds measurements over those outcomes. Lesson 003 will use one of those measurements—especially expectation and variance—to summarize a distribution without confusing the summary with a promise about the next run.
The same pattern appears in telemetry, experiments, machine learning features, and business metrics: first define what happened, then decide which measurement makes the question visible.
Resources
- [COURSE] MIT OpenCourseWare: Introduction to Probability — Focus: Follow the progression from sample spaces to discrete and continuous random variables.
- [REFERENCE] MIT 18.05: Probability Terminology and Examples — Focus: Review random numerical outcomes, probability functions, and small distribution tables.
- [COURSE] MIT OpenCourseWare: Fundamentals of Probability — Focus: Use the later sections as an optional deeper path for random variables, expectations, and stochastic processes.
Key Takeaways
- A random variable is a function from an outcome to a quantity chosen for a question.
- Different random variables can measure different properties of the same sample space.
- A discrete distribution places probability mass on separated values; its CDF accumulates probability up to a threshold.
- A continuous measurement is usually interpreted through intervals and density, not probability at one exact point.
- A variable simplifies a model by discarding information. That is useful only when the discarded distinctions do not matter for the question.
← Back to Probability, Random Processes, and Statistical Thinking