Written and maintained by CASRAI Editorial Board
Last updated
A random variable is a characteristic of interest whose value is determined by chance and can be recorded as a number, such as the number of times a newborn wakes its mother after midnight in one week. This guide defines the idea, shows the formulas for the mean and standard deviation, and works one complete example with the arithmetic written out, so you can check every number.
What is a random variable? Definition in one paragraph
OpenStax Introductory Statistics defines a random variable (RV) as “a characteristic of interest in a population being studied.” In plain terms, it is a rule that gives each possible outcome of a chance process a number, and it is usually written as a capital letter such as X. A particular value it can take is written in lower case, x. The statement P(X = x) reads “the probability that the random variable X takes the value x.” OpenStax adds that you learn which value X takes only after performing the experiment, and that the values need not be numbers (hair colour is its example), though this guide works with numerical ones. Random variables come in two kinds: discrete, where the possible values can be listed, and continuous, where the values fill an interval.
Random variable examples: discrete and continuous
Discrete random variables
NIST/SEMATECH defines a discrete probability function through a list of values and their probabilities, and calls these probability mass functions. Counts are the usual case. The OpenStax example is X = the number of times per week a newborn baby’s crying wakes its mother after midnight, which can only be 0, 1, 2, 3, 4 or 5 in the survey described below. Its distribution is described by a probability mass function (PMF), the name NIST uses for discrete probability functions.
Continuous random variables
A continuous random variable can take any value in an interval, for example the time between two events. NIST gives the rule: the probability that the value lies between a and b is the integral of the density function f(x) from a to b, the integral of f over all real numbers is 1, and “the probability at a single point is always zero.” Continuous functions are called probability density functions (PDF). For the best-known continuous case, see the guide to the probability distribution function of the normal distribution.
Random variable formula: mean and standard deviation
For a discrete random variable, the two rules every distribution must obey come from OpenStax: each probability is between 0 and 1 inclusive, and the probabilities sum to 1. NIST writes the same statement as the sum of all pj equal to 1. The mean (expected value) and standard deviation are then defined as follows, again following OpenStax Section 4.2.
- Mean: μ = E(X) = Σ [ x · P(x) ]. Here x is each possible value, P(x) is its probability, and Σ adds the products over all values. OpenStax calls this the expected value, the long-term average over many repetitions.
- Standard deviation: σ = √ Σ [ (x − μ)2 · P(x) ]. OpenStax defines it as the square root of the variance, so the variance is σ2 = Σ [ (x − μ)2 · P(x) ].
For the continuous case the sums become integrals of the density, as in the NIST statement above. The expected value formula guide covers more variants of the mean, and binomial distribution expected value shows the shortcut for one common discrete random variable.
Worked example with real numbers
OpenStax Example 4.1 surveyed 50 mothers. Let X = the number of times per week a newborn baby’s crying wakes its mother after midnight. The reported distribution is below; the mean and standard deviation are computed here from it.
| x | Mothers | P(x) | x · P(x) | (x − 2.1)2 · P(x) |
|---|---|---|---|---|
| 0 | 2 | 2/50 = 0.04 | 0.00 | 4.41 × 0.04 = 0.1764 |
| 1 | 11 | 11/50 = 0.22 | 0.22 | 1.21 × 0.22 = 0.2662 |
| 2 | 23 | 23/50 = 0.46 | 0.92 | 0.01 × 0.46 = 0.0046 |
| 3 | 9 | 9/50 = 0.18 | 0.54 | 0.81 × 0.18 = 0.1458 |
| 4 | 4 | 4/50 = 0.08 | 0.32 | 3.61 × 0.08 = 0.2888 |
| 5 | 1 | 1/50 = 0.02 | 0.10 | 8.41 × 0.02 = 0.1682 |
| Total | 50 | 1.00 | μ = 2.10 | σ2 = 1.0500 |
- Validity check. Every probability is between 0 and 1, and 2 + 11 + 23 + 9 + 4 + 1 = 50, so the probabilities sum to 50/50 = 1.
- Mean. 0 + 0.22 + 0.92 + 0.54 + 0.32 + 0.10 = 2.10 (equivalently 105/50).
- Variance. 0.1764 + 0.2662 + 0.0046 + 0.1458 + 0.2888 + 0.1682 = 1.0500. Cross-check: E(X2) = (0 + 11 + 92 + 81 + 64 + 25)/50 = 273/50 = 5.46, and 5.46 − 2.12 = 5.46 − 4.41 = 1.05.
- Standard deviation. σ = √1.05 = 1.0247, about 1.02.
Interpretation: how to read the result
The mean of 2.10 does not have to be a value the variable can take; here nobody wakes their mother 2.1 times. It is the long-run average over many repetitions. The standard deviation of about 1.02 says typical weeks differ from that average by roughly one waking.
| Question | Discrete random variable | Continuous random variable |
|---|---|---|
| Possible values | A countable list (NIST: values with individual probabilities) | Any value in an interval |
| Function used | Probability mass function, P(X = x) | Probability density function, f(x) |
| Probability of one exact value | Can be above zero | Always zero (NIST) |
| Total probability of 1 from | A sum over all values | An integral over all real numbers |
Assumptions and common mistakes
- Probabilities that do not sum to 1. A table whose probabilities add to anything other than 1 is not a valid distribution. Always run the check first.
- Reading a density as a probability. A value of f(x) is a height of the curve, not P(X = x). Probabilities for continuous variables come from areas, and NIST states that a single point has probability zero.
- Mixing up the variable and its observed values. X is the quantity before you look; x is a value it can take, and a sample of observations is a different object.
- Confusing a random variable with a study variable. Independent and dependent variables describe the role a measure plays in a design; a random variable describes the chance behaviour of a numeric outcome. See independent vs dependent variable and confounding variable.
- Treating discrete and continuous data alike. Which type you have decides the method; quantitative data examples shows the same split for measured data.
How to report it (APA example)
Define the variable in words, then give the mean and standard deviation with the same number of decimals throughout, for example:
Let X be the number of times per week a newborn’s crying woke its mother after midnight. In the survey of 50 mothers, the mean was M = 2.10 and the standard deviation was SD = 1.02.
Here the mean and standard deviation are calculated from the distribution, so state that they come from the probability distribution rather than from raw individual data when that is the case.
How to calculate it in Excel, R or Python
Excel
Put the values x in A2:A7 and the probabilities in B2:B7. Mean: =SUMPRODUCT(A2:A7,B2:B7) gives 2.1. Variance: =SUMPRODUCT((A2:A7-SUMPRODUCT(A2:A7,B2:B7))^2,B2:B7) gives 1.05, and standard deviation is =SQRT( that cell ), about 1.0247.
R
x <- 0:5
p <- c(2, 11, 23, 9, 4, 1) / 50
mu <- sum(x * p) # 2.1
v <- sum((x - mu)^2 * p) # 1.05
sqrt(v) # 1.0247
Python
import numpy as np
x = np.arange(0, 6)
p = np.array([2, 11, 23, 9, 4, 1]) / 50
mu = (x * p).sum() # 2.1
var = ((x - mu) ** 2 * p).sum() # 1.05
print(mu, var, var ** 0.5) # 2.1 1.05 1.0247...
FAQ
What is a random variable?
It is a characteristic whose value depends on chance; in this guide it is recorded as a number. OpenStax defines it as “a characteristic of interest in a population being studied.” It is written as a capital letter, and its possible values and their probabilities make up its probability distribution.
What is the difference between a discrete and a continuous random variable?
A discrete variable has listable values and a probability mass function whose probabilities sum to 1. A continuous variable takes values in an interval, has a density function that integrates to 1, and has probability zero at any single point (NIST).
What is the formula for the expected value of a random variable?
For a discrete variable, μ = Σ [ x · P(x) ], the sum of each value times its probability. In the worked example this gives 2.10.
Can the expected value be a number the variable never takes?
Yes. It is a long-run average, so 2.10 is a valid mean for a variable that only takes whole numbers.
Sources
- OpenStax, Introductory Statistics 2e, Section 4.1, probability distribution function for a discrete random variable: https://openstax.org/books/introductory-statistics-2e/pages/4-1-probability-distribution-function-pdf-for-a-discrete-random-variable (checked 2026-10-29).
- OpenStax, Introductory Statistics 2e, Section 4.2, mean or expected value and standard deviation: https://openstax.org/books/introductory-statistics-2e/pages/4-2-mean-or-expected-value-and-standard-deviation (checked 2026-10-29).
- OpenStax, Introductory Statistics 2e, Chapter 4 Key Terms: https://openstax.org/books/introductory-statistics-2e/pages/4-key-terms (checked 2026-10-29).
- NIST/SEMATECH e-Handbook of Statistical Methods, Section 1.3.6.1, Related Distributions: https://www.itl.nist.gov/div898/handbook/eda/section3/eda361.htm (checked 2026-10-29).








