Skip to the content of the web site.

Project AA.2: Discrete uniform distribution

A random variable that chooses an integer from $a$ to $b$, inclusive, with each integer appearing with equal probability is said to be a discrete uniform distribution.

For example, flipping a fair coin has a uniform distribution from $0$ to $1$ where $0$ represents "heads", rolling a fair die has a uniform distribution from $1$ to $6$, spinning a roulette wheel has a uniform distribution from $0$ to $36$ (unless you are in the United States, where they include a second "$00$" to increase the profits taken by the house, in which case you can consider it a uniform distribution from $-1$ to $36$ where "$-1$" represents "$00$").

Implement a function that returns an integer between $a$ and $b$ with equal likelihood.

Use an assertion to ensure that the argument $a$ is less than or equal to $b$.

The rand(), found in the cstdlib library, returns an integer between 0 and RAND_MAX, inclusive.

#include <cstdlib>
#include <cassert>

// Function declaration
int discrete_uniform( int a, int b );

Class implementation

Implement a class Discrete_uniform_distribution where:

  • The constructor Discrete_uniform_distribution::Discrete_uniform_distribution( int a, int b ); takes arguments $a$ and $b$ and creates an instance that generates random values that follow the discrete uniform distribution.
  • The member function int Discrete_uniform_distribution::operator()() const; returns a value between $a$ and $b$, inclusive, with equal probability.

Comment on roulette

A roulette wheel has the numbers $0$ through $36$, but most bets involve numbers between $1$ and $36$. For example, betting on "EVEN" does not include $0$, betting on "LOW" includes $1$ through $18$ but not $0$, etc. Thus, in general, the house makes its profit when a $0$ appears on the wheel, and therefore, the house will take on average $\frac{1}{37}$ of all bets, or $2.70 for each $100 bet.

An American roulette wheel has a $0$ and a $00$, in which case, the house wins if either of these appears on the wheel. Thus, American gambling houses collect on average $\frac{2}{38}$ of all bets, or $5.26 for each $100 bet.