Generating a Uniform Distribution over {1, 2, 3} from an Arbitrary RNG
You have access to a random number generator that produces integers uniformly over $\{1, 2, \ldots, n\}$ for some given $n$. You want to produce outputs that are uniformly distributed over $\{1, 2, 3\}$ -- each value appearing with probability exactly
How would you design this procedure? Consider two cases: 1. The generator produces integers from $\{1, \ldots, 6\}$ (a fair die). 2. The generator produces integers from $\{1, \ldots, 5\}$.
For the general case, describe an algorithm that works for any $n$ and prove that it produces exact uniform probabilities.
Hints
- You cannot perfectly split $n$ equally-likely outcomes into exactly 3 groups unless $3 \mid n$. The standard fix is rejection sampling: throw away some outcomes to make the effective sample space divisible by 3.
- Find the largest multiple of 3 that is at most $n$ -- call it $m = 3\lfloor n/3 \rfloor$. Accept draws in $\{1, \ldots, m\}$ and reject the rest. Conditional on acceptance, the draw is uniform over a set of size $m$, which divides evenly by 3.
- Map an accepted draw $X$ to output $((X-1) \bmod 3) + 1$. Verify: for $n=6$, $m=6$, $P(\text{output}=k) = 2/6 = 1/3$ for each $k$. For $n=5$, $m=3$, $P(\text{output}=k) = 1/3$ conditional on acceptance, and acceptance probability is $3/5$.
Worked Solution
How to Think About It: The fundamental tension here is that
Key Insight: If $3 \mid n$, every integer in $\{1, \ldots, n\}$ can be mapped to $\{1,2,3\}$ via $((x-1) \bmod 3) + 1$ with no rejection needed. If $3 \nmid n$, restrict to the range $\{1, \ldots, 3\lfloor n/3 \rfloor\}$ and reject values above that threshold.
The Method:
- Let $m = 3\lfloor n/3 \rfloor$ (the largest multiple of 3 that is $\leq n$).
- Draw $X \sim \text{Uniform}\{1, \ldots, n\}$.
- If $X > m$, discard $X$ and return to step 2.
- If $X \leq m$, return $((X - 1) \bmod 3) + 1$.
Why it works: Conditional on acceptance ($X \leq m$), the variable $X$ is uniform on $\{1, \ldots, m\}$. Since $3 \mid m$, exactly $m/3$ values in $\{1, \ldots, m\}$ map to each of $\{1, 2, 3\}$. So each output value has conditional probability $\frac{m/3}{m} = 1/3$. Exact uniformity.
Case 1: Fair die ($n = 6$). $6 = 3 \times 2$, so $m = 6$ and there is no rejection. Map: $1, 2 \to 1, \quad 3, 4 \to 2, \quad 5, 6 \to 3$ All 6 outcomes are used; each of $\{1,2,3\}$ gets probability
Case 2: $n = 5$ (rand over $\{1,2,3,4,5\}$). $\lfloor 5/3 \rfloor = 1$, so $m = 3$. Accept $X \in \{1, 2, 3\}$, reject $X \in \{4, 5\}$. - Map:
Practical Considerations: - Efficiency: The rejection probability is at most