liveBook · Manning

Bernoulli distribution

As explained in Fabulous Adventures in Data Structures and Algorithms

A Bernoulli distribution is a probability distribution over two outcomes, conventionally represented by 0 and 1, with an arbitrary probability for one outcome.

A Bernoulli distribution models one binary choice: 0 or 1. Read 0 as tails and 1 as heads, then assign each outcome its own weight. Equal weights produce a fair coin; unequal weights produce an unfair one. section 12.5 section 12.13

An integer-weight implementation makes the odds explicit and handles degenerate coins cleanly:

  • Return a singleton distribution when either weight is zero: the result is always 0 or always 1. section 12.5
  • Reduce positive weights by their greatest common divisor, then sample uniformly from their total and partition the range between 0 and 1. section 12.5

A coin with 5:7 odds

var coin = Bernoulli.Distribution(5, 7); int flip = coin.Sample(); The implementation reduces no further, samples uniformly from 0 through 11, and returns 0 for five positions and 1 for seven. Thus the coin produces 1 more often than 0 without exposing a direct Random call. section 12.5

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains representation, degenerate cases, GCD normalization, and uniform sampling, but stops short of broader probability theory.
Examples
Uses fair, unfair, double-headed, double-tailed, and five-to-seven weighted coins as implementation examples.
What it assumes
Assumes familiarity with C# classes, interfaces, generic distributions, integer weights, and basic GCD computation.
What you can do after
Build it from scratch in C# with integer weights, normalized odds, singleton edge cases, and a Sample operation.
How it connects
Connects Bernoulli distributions to discrete uniform distributions, singleton distributions, categorical distributions, integer weights, and GCD.

Also explained in

Compare how 2 books explain Bernoulli distribution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.