Probability for Computer Science Beginners

Reason about random outcomes with sample spaces, events, and conditional probability.

By Rishipal Singh · · 6 min read

A probability tree shows outcomes branching into weighted possibilities.
Visual guide: A probability tree shows outcomes branching into weighted possibilities.

Probability gives us a language for uncertainty. It helps reason about randomized algorithms, reliability, experiments, and systems where outcomes are not fully predictable.

Outcomes, sample spaces, and events

A sample space lists possible outcomes of an experiment. When rolling a fair six-sided die, it is {1, 2, 3, 4, 5, 6}. An event is a set of outcomes, such as rolling an even number. In this example, the event's probability is 3/6 = 1/2.

For equally likely outcomes, probability is the number of favorable outcomes divided by the total number of outcomes. When outcomes are not equally likely, use their actual probabilities instead.

Conditional probability

The probability of event A given event B is written P(A | B). It asks how likely A is after learning that B occurred. For example, in a deck of cards, the chance of drawing a king changes if you already know the card is a face card.

Carefully define what information is known. Confusing P(A | B) with P(B | A) is a common source of mistakes because the conditions are different.

Expected value

The expected value is a probability-weighted average of possible outcomes. It does not predict the next individual result, but it describes the long-run average across many repetitions.

In computer science, probability helps analyze randomized algorithms and model failures. State your assumptions about randomness and independence; conclusions are only as trustworthy as the model behind them.

Practice: compute a conditional probability

A fair six-sided die is rolled. Given that the result is even, what is the probability it is greater than 3?

Reveal the explanation

The condition leaves {2, 4, 6}. Two of those three outcomes are greater than 3, so the conditional probability is 2/3.