---
title: Two-Coin Bernoulli Factory Algorithm
url: https://www.emergentmind.com/topics/two-coin-bernoulli-factory-algorithm
type: topic
---

# Two-Coin Bernoulli Factory Algorithm

A two-coin Bernoulli factory is an algorithm that is given sample access to two coins with unknown biases and must output a single Bernoulli random variable with success probability equal to a prescribed function of those biases. In the standard formulation, one may flip the input coins as often as desired, use auxiliary randomness, and require exactness rather than approximation. Within this framework, the literature contains both a specific “2-coin algorithm” for simulating a ratio of the form $\frac{c_1p_1}{c_1p_1+c_2p_2}$ and more general two-input factories for linear targets such as $C_1p_1+C_2p_2$ and for broader multiparameter classes of functions [1507.00843] [1709.07710].

## 1. Problem setting and terminological scope

The general Bernoulli-factory problem asks for an algorithm that uses coin flips together with auxiliary randomness to flip a single coin with probability $f(p)$ of heads when only a coin with unknown probability $p$ of heads is available. In the two-input setting, the corresponding task is to simulate a Bernoulli variable with mean $f(p_1,p_2)$ given only the ability to draw i.i.d. Bernoulli samples from two unknown biases $p_1$ and $p_2$ [1507.00843] [2202.07216].

Across the two-input literature, three target forms are especially prominent. The first is the linear target
$$
f(p_1,p_2)=C_1p_1+C_2p_2,
$$
typically under a known constraint
$$
r=C_1p_1+C_2p_2\le M<1.
$$
The second is the ratio target
$$
p=\frac{c_1p_1}{c_1p_1+c_2p_2},
$$
which appears directly in Barker-type accept/reject steps for intractable likelihoods. The third is the fully general multiparameter problem of determining which continuous functions $f:[0,1]^2\to[0,1]$ admit a finite-expected-time Bernoulli factory [1709.07710] [2202.07216].

| Target | Assumptions stated in the source material | Representative source |
|---|---|---|
| $C_1p_1+C_2p_2$ | $C_1p_1+C_2p_2\le M<1$ | [1507.00843] |
| $\frac{c_1p_1}{c_1p_1+c_2p_2}$ | $c_1,c_2>0$; exact simulation of Bernoulli$(p_1)$ and Bernoulli$(p_2)$ | [1709.07710] |
| General $f(p_1,p_2)$ | continuity and polynomial bounds for both $f$ and $1-f$ on faces of $[0,1]^2$ | [2202.07216] |

A common misconception is that a Bernoulli factory requires closed-form evaluation of $p_1$ and $p_2$. The two-coin constructions described here do not: the operative assumption is access to Bernoulli$(p_i)$ draws, not numerical knowledge of the biases themselves [1709.07710].

## 2. Linear two-coin factory for $C_1p_1+C_2p_2$

For the linear problem, the objective is to simulate a Bernoulli random variable $B$ with
$$
P(B=1)=f(p_1,p_2)=C_1p_1+C_2p_2=r,
$$
under the assumption that $r\le M<1$ is known, often with $M\le 1/2$ or $M=1-\epsilon$. The notation $C=C_1+C_2$ is used throughout. The algorithm follows the same three-step “small-$r$” structure as in the univariate case [1507.00843].

The first step is logistic thinning. One produces
$$
Y\sim \mathrm{Bern}\!\Bigl(\frac{r}{1+r}\Bigr)
$$
by embedding $C_1p_1+C_2p_2$ as the rate of a thinned Poisson process. The subroutine `LogisticBF2` simulates two independent Poisson processes of rates $C_1$ and $C_2$, thins them by the two coins, and compares the earliest retained event against an independent $\mathrm{Exp}(1)$ clock. If
$$
T'=\min\{\text{points of }P_1\cup P_2\}\sim \mathrm{Exp}(r),\qquad A\sim \mathrm{Exp}(1),
$$
then
$$
P(T'<A)=\frac{r}{1+r},
$$
which is exactly the success probability required for the logistic coin [1507.00843].

The second step is a splitting construction based on a constant $\beta>1$. The source material writes
$$
r=\frac{1}{\beta}\,\frac{\beta r}{1+\beta r}
+\Bigl(1-\frac1\beta\Bigr)\,\frac{\beta r}{1+\beta r}\,
f\!\Bigl(\frac{\beta}{\beta-1}p_1,\frac{\beta}{\beta-1}p_2\Bigr).
$$
Operationally, one samples
$$
Y\sim \mathrm{Bern}\Bigl(\frac{\beta r}{1+\beta r}\Bigr),\qquad U\sim \mathrm{Bern}(1/\beta).
$$
If $Y=0$ the algorithm returns $0$; if $Y=1$ and $U=1$ it returns $1$; otherwise it recurses with inflated coefficients
$$
C_1'=\frac{\beta}{\beta-1}C_1,\qquad C_2'=\frac{\beta}{\beta-1}C_2.
$$
In the pseudocode, $\beta=1/(1-2M)$ is chosen so that $\beta r\le 1-M<1$, and the implementation guide updates
$$
M\leftarrow \frac{\beta M}{\beta-1}
$$
before recursion [1507.00843].

The third step is tail recursion and termination. The probability of another recursive call shrinks by a fixed factor smaller than $1$, so the procedure halts after a finite random number of recursions with probability $1$. For very small $r$, the routine may instead call the univariate `SmallR_UnivariateBF` with $C=C_1+C_2$, effectively treating $r=C_1p_1+C_2p_2$ as a single weighted input [1507.00843].

## 3. Correctness, stopping behavior, and efficiency

The linear two-coin factory is unbiased because the splitting identity decomposes $r$ into an immediate-accept term and a recursive remainder. The same source gives the recursion probability bound
$$
\Bigl(1-\frac1\beta\Bigr)\,\frac{\beta r}{1+\beta r}
\le \frac{\beta-1}{\beta}\,M<1,
$$
which yields almost-sure halting [1507.00843].

Its most prominent feature is the small-$r$ cost profile. Let $C=C_1+C_2$ and $r=C_1p_1+C_2p_2$. For $r\le M<1/2$, the source states that
$$
E[\#\mathrm{flips}]
\le
\frac{C}{(1-2M)(1+r)}
+
r\times \frac{15.2\,C}{1-2M+r}.
$$
In particular,
$$
E[N]\sim \frac{C}{1+r}\longrightarrow C
\qquad\text{as }r\to 0,
$$
so to first order one uses $C$ flips. The same source further states that information-theoretic lower bounds via the Cramér–Rao argument in §6 show that no Bernoulli-factory can use fewer than approximately $C$ flips in the small-$r$ regime; on that basis, the two-coin linear factory is described as first-order optimal [1507.00843].

The large-$r$ regime is treated by imposing a known cutoff $M=1-\epsilon$. Under that assumption, the expected cost is stated to be $O(C/\epsilon)$, improving substantially on older $O(C/\epsilon^2)$ factories. The same implementation notes recommend that, in practice, once $r$ becomes say $>0.2$, one may switch to the classical Nacu–Peres factory for functions of the form $\min\{r,1-\epsilon\}$; the resulting hybrid is stated to use at most approximately $7.6\,C/\epsilon$ flips when $r\le 1-\epsilon$ [1507.00843].

This efficiency claim is specific to the linear target. A plausible implication is that “two-coin Bernoulli factory” should not be treated as a single complexity class: the expected cost depends sharply on the functional form of the target, the boundary regime, and the availability of structure such as linearity.

## 4. The ratio-form 2-coin algorithm and Barker acceptance

A second, widely cited meaning of “two-coin algorithm” is the Bernoulli-factory subroutine for
$$
p=\frac{c_1p_1}{c_1p_1+c_2p_2},
$$
where $c_1,c_2>0$ are known constants and $p_1,p_2\in[0,1]$ are unknown probabilities from which Bernoulli draws can be simulated [1709.07710].

The algorithm is iterative and exceptionally simple. In each loop, one first flips
$$
C_1\sim \mathrm{Bern}\Bigl(\frac{c_1}{c_1+c_2}\Bigr).
$$
If $C_1=1$, one flips a Bernoulli$(p_1)$ coin and returns $1$ if that coin is $1$; otherwise the loop continues. If $C_1=0$, one flips a Bernoulli$(p_2)$ coin and returns $0$ if that coin is $1$; otherwise the loop continues. The source formulates correctness by defining the one-iteration output probabilities
$$
\rho=\frac{c_1}{c_1+c_2}p_1,\qquad
\sigma=\frac{c_2}{c_1+c_2}p_2,
$$
with repeat probability $r=1-(\rho+\sigma)$. Summing the geometric series gives
$$
\Pr(Y=1)=\sum_{k=1}^\infty r^{k-1}\rho
=\frac{\rho}{1-r}
=\frac{c_1p_1}{c_1p_1+c_2p_2},
$$
and similarly
$$
\Pr(Y=0)=\frac{c_2p_2}{c_1p_1+c_2p_2}.
$$
Hence the returned variable is exactly $\mathrm{Bern}(p)$ [1709.07710].

The expected cost is also explicit. The termination probability per loop is
$$
\rho+\sigma=\frac{c_1p_1+c_2p_2}{c_1+c_2},
$$
so the number of loops is geometric with mean
$$
\mathbb{E}[T]=\frac{c_1+c_2}{c_1p_1+c_2p_2}.
$$
Since each loop uses exactly one “$C_1$-coin” plus one “$C_2$-coin,” the expected total number of coin flips is
$$
\mathbb{E}[\#\text{coins}]
=
2\,\frac{c_1+c_2}{c_1p_1+c_2p_2}.
$$
The source notes that the cost grows as $1/p$ when $p\to 0$ or $1/(1-p)$ when $p\to 1$ [1709.07710].

In Bayesian computation, this ratio form is used to realize Barker’s acceptance rule for intractable target densities. With
$$
c_1=\pi(\phi)q(\phi,\theta),\qquad
c_2=\pi(\theta)q(\theta,\phi),
$$
the Barker acceptance probability can be written as
$$
\alpha_B(\theta,\phi)=\frac{c_1p_1}{c_1p_1+c_2p_2},
$$
where $p_1,p_2$ arise from unbiased simulation subroutines for the unknown target densities. The source emphasizes that this permits implementation of the “marginal Barker’s” instead of the extended state space pseudo-marginal Metropolis–Hastings. It also notes that Peskun ordering gives $\alpha_B\le \alpha_{MH}$, and that the asymptotic variance of Barker is at worst twice that of Metropolis–Hastings, plus a $\mathrm{Var}_\pi$ term [1709.07710].

## 5. General two-input theory

The multiparameter theory of Bernoulli factories places the two-coin problem in a complete characterization framework. For $f:[0,1]^2\to[0,1]$, a Bernoulli factory that terminates almost surely with finite expectation everywhere exists if and only if two conditions hold: first, $f$ is continuous on $[0,1]^2$; second, both $f$ and $1-f$ are polynomially bounded on every face of the square in the sense specified by the boundary monomials associated with the face decomposition [2202.07216].

The source summarizes this criterion in words: on each face where $f$ does not vanish identically, it can never dip below a fixed constant times the corresponding boundary-monomial to some power, and likewise for $1-f$. It states that this exactly generalizes the one-coin Keane–O’Brien condition
$$
\min(p,1-p)^m\lesssim f(p)\lesssim 1-\min(p,1-p)^m.
$$
This result is a structural characterization rather than a special-purpose construction for a single target [2202.07216].

For implementable $f$, the source gives a general two-coin factory based on “recursive splitting plus Boolean test.” One precomputes a threshold-test sample size $t$ and a helper-coin probability $q=1/4$. A finite-depth subroutine $G$ flips each input coin exactly $t$ times, forms $\bar X_t=(i/t,j/t)$, computes the deterministic predicate
$$
D=1[f(i/t,j/t)\ge 1/2],
$$
and outputs $D$. The full factory then uses a geometric helper-coin mechanism with parameter $1/4$ to mix calls to $G$. The source states that the expected number of $G$-calls is $4$, each call to $G$ uses $2t$ flips of the unknown coins, and therefore the expected total number of flips is $8t$ [2202.07216].

The same exposition gives two illustrative examples. For
$$
f(p_1,p_2)=p_1p_2,
$$
the direct factory is simply: flip coin$_1$ once; if $1$ then flip coin$_2$ once and output that result; otherwise output $0$. The source states that the complexity is at most $2$ flips. It also discusses
$$
f(p_1,p_2)=\frac{p_1}{p_1+p_2}
$$
on the domain $K=\{(p_1,p_2):p_1+p_2>0\}$ as a further worked example [2202.07216].

A plausible implication of this characterization is that two-coin factories are best viewed not as isolated tricks but as instances of a boundary-sensitive realizability theory. The central obstruction is not the number of unknown coins by itself, but the interaction between continuity, boundary behavior, and exact simulation.

## 6. Extensions, robustness, and quantum implementations

Two-coin randomness-processing ideas also appear in adjacent settings that are not limited to Bernoulli targets. One example is the mechanism of jointly controlled lotteries with two biased coins. There, the goal is to implement a distribution on a finite set of elements so that even if the outcomes of one of the coins are determined by an adversary, the final distribution remains unchanged. The source describes both a fixed-length procedure that $\epsilon$-approximates any target distribution $\pi$ and an unbounded-length, almost-surely finite procedure that implements $\pi$ exactly. It states that the fixed-length version uses $O(\epsilon^{-2})$ flips per coin, while the unbounded exact version uses expected $O(1/c_1)$ flips per coin with
$$
c_1=\min\{pq,p(1-q),(1-p)q,(1-p)(1-q)\},
$$
and that both schemes terminate with probability $1$ even if one coin is fully controlled [1803.00802].

A different extension is quantum. Randomness processing in a Bernoulli factory has been identified as a task for which quantum technology can be advantageous. An experimental photonic realization reports two quantum Bernoulli factories, one utilising quantum coherence and single-qubit measurements and the other using quantum coherence and entangling measurements of two qubits. The source states that the former consumes three orders of magnitude fewer resources than the best known classical method, while entanglement offers a further five-fold reduction. It further suggests applications to the simulation of stochastic processes and sampling tasks [1807.04297].

These directions clarify the broader significance of the two-coin Bernoulli-factory idea. In one direction, the framework supports exact accept/reject decisions in MCMC with intractable likelihoods; in another, it yields nearly optimal classical factories for linear functions of multiple unknown biases; in another, it connects to robust distributed randomization and to experimentally demonstrated quantum-enhanced randomness processing [1507.00843] [1709.07710] [1807.04297].

Source: https://www.emergentmind.com/topics/two-coin-bernoulli-factory-algorithm