Random variables (discrete vs continuous)

Log in to access the full course.

What is a random variable?

A random variable is a variable whose value is determined by a random process. Rather than having a fixed value, it takes on different values with certain probabilities.

Think of rolling a die. Before you roll, the outcome is uncertain — it could be 1, 2, 3, 4, 5, or 6. A random variable XX represents that uncertain outcome. We write P(X=3)P(X = 3) to mean "the probability that the die lands on 3."

Random variables come in two types: discrete and continuous.

Discrete random variables

A discrete random variable takes on a countable set of values — usually whole numbers.

Examples:

  • The number of heads in 3 coin flips: X{0,1,2,3}X \in \{0, 1, 2, 3\}
  • The number of customers arriving in an hour: X{0,1,2,}X \in \{0, 1, 2, \ldots\}
  • The result of rolling a die: X{1,2,3,4,5,6}X \in \{1, 2, 3, 4, 5, 6\}

For a discrete random variable, we describe its behavior with a probability mass function (PMF): a list of probabilities for each possible value. The probabilities must sum to 1:

xP(X=x)=1\sum_x P(X = x) = 1

Example — fair die:

xx123456
P(X=x)P(X=x)1/61/61/61/61/61/6

Continuous random variables

A continuous random variable can take any value within a range — not just whole numbers.

Examples:

  • A person's height: any value in, say, [1.4,2.2][1.4,\, 2.2] metres
  • The time until the next bus arrives: any value 0\geq 0
  • The temperature at noon tomorrow

Because there are infinitely many possible values, the probability of any single exact value is zero: P(X=1.756)=0P(X = 1.756\ldots) = 0. Instead, we talk about probability over intervals.

For continuous random variables we use a probability density function (PDF) f(x)f(x). The probability that XX falls between aa and bb is the area under the curve:

P(aXb)=abf(x)dxP(a \leq X \leq b) = \int_a^b f(x)\, dx

The total area under the PDF equals 1:

f(x)dx=1\int_{-\infty}^{\infty} f(x)\, dx = 1

Expected value

For both types, the expected value E[X]E[X] is the long-run average — the value you would expect on average over many repetitions.

For a discrete variable:

E[X]=xxP(X=x)E[X] = \sum_x x \cdot P(X = x)

For the fair die: E[X]=116+216++616=3.5E[X] = 1 \cdot \frac{1}{6} + 2 \cdot \frac{1}{6} + \cdots + 6 \cdot \frac{1}{6} = 3.5

Quick comparison

DiscreteContinuous
ValuesCountable (often integers)Any value in a range
Probabilities described byPMFPDF
P(X=x)P(X = x)Can be non-zeroAlways zero
ExampleDie roll, coin flipsHeight, temperature