Papers
Topics
Authors
Recent
Search
2000 character limit reached

Two-Coin Bernoulli Factory Algorithm

Updated 4 July 2026
  • Two-coin Bernoulli factory algorithm is a method that uses i.i.d. coin flips from two unknown biases to simulate a Bernoulli variable with a prescribed function.
  • It employs techniques such as logistic thinning and recursive splitting to efficiently simulate targets like linear combinations and ratios.
  • Its efficiency, optimal flip usage in low-probability regimes, and extensions to quantum and robust distributed settings underscore its practical significance.

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 c1p1c1p1+c2p2\frac{c_1p_1}{c_1p_1+c_2p_2} and more general two-input factories for linear targets such as C1p1+C2p2C_1p_1+C_2p_2 and for broader multiparameter classes of functions (Huber, 2015, Gonçalves et al., 2017).

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)f(p) of heads when only a coin with unknown probability pp of heads is available. In the two-input setting, the corresponding task is to simulate a Bernoulli variable with mean f(p1,p2)f(p_1,p_2) given only the ability to draw i.i.d. Bernoulli samples from two unknown biases p1p_1 and p2p_2 (Huber, 2015, Leme et al., 2022).

Across the two-input literature, three target forms are especially prominent. The first is the linear target

f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,

typically under a known constraint

r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.

The second is the ratio target

p=c1p1c1p1+c2p2,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 C1p1+C2p2C_1p_1+C_2p_20 admit a finite-expected-time Bernoulli factory (Gonçalves et al., 2017, Leme et al., 2022).

Target Assumptions stated in the source material Representative source
C1p1+C2p2C_1p_1+C_2p_21 C1p1+C2p2C_1p_1+C_2p_22 (Huber, 2015)
C1p1+C2p2C_1p_1+C_2p_23 C1p1+C2p2C_1p_1+C_2p_24; exact simulation of BernoulliC1p1+C2p2C_1p_1+C_2p_25 and BernoulliC1p1+C2p2C_1p_1+C_2p_26 (Gonçalves et al., 2017)
General C1p1+C2p2C_1p_1+C_2p_27 continuity and polynomial bounds for both C1p1+C2p2C_1p_1+C_2p_28 and C1p1+C2p2C_1p_1+C_2p_29 on faces of f(p)f(p)0 (Leme et al., 2022)

A common misconception is that a Bernoulli factory requires closed-form evaluation of f(p)f(p)1 and f(p)f(p)2. The two-coin constructions described here do not: the operative assumption is access to Bernoullif(p)f(p)3 draws, not numerical knowledge of the biases themselves (Gonçalves et al., 2017).

2. Linear two-coin factory for f(p)f(p)4

For the linear problem, the objective is to simulate a Bernoulli random variable f(p)f(p)5 with

f(p)f(p)6

under the assumption that f(p)f(p)7 is known, often with f(p)f(p)8 or f(p)f(p)9. The notation pp0 is used throughout. The algorithm follows the same three-step “small-pp1” structure as in the univariate case (Huber, 2015).

The first step is logistic thinning. One produces

pp2

by embedding pp3 as the rate of a thinned Poisson process. The subroutine LogisticBF2 simulates two independent Poisson processes of rates pp4 and pp5, thins them by the two coins, and compares the earliest retained event against an independent pp6 clock. If

pp7

then

pp8

which is exactly the success probability required for the logistic coin (Huber, 2015).

The second step is a splitting construction based on a constant pp9. The source material writes

f(p1,p2)f(p_1,p_2)0

Operationally, one samples

f(p1,p2)f(p_1,p_2)1

If f(p1,p2)f(p_1,p_2)2 the algorithm returns f(p1,p2)f(p_1,p_2)3; if f(p1,p2)f(p_1,p_2)4 and f(p1,p2)f(p_1,p_2)5 it returns f(p1,p2)f(p_1,p_2)6; otherwise it recurses with inflated coefficients

f(p1,p2)f(p_1,p_2)7

In the pseudocode, f(p1,p2)f(p_1,p_2)8 is chosen so that f(p1,p2)f(p_1,p_2)9, and the implementation guide updates

p1p_10

before recursion (Huber, 2015).

The third step is tail recursion and termination. The probability of another recursive call shrinks by a fixed factor smaller than p1p_11, so the procedure halts after a finite random number of recursions with probability p1p_12. For very small p1p_13, the routine may instead call the univariate SmallR_UnivariateBF with p1p_14, effectively treating p1p_15 as a single weighted input (Huber, 2015).

3. Correctness, stopping behavior, and efficiency

The linear two-coin factory is unbiased because the splitting identity decomposes p1p_16 into an immediate-accept term and a recursive remainder. The same source gives the recursion probability bound

p1p_17

which yields almost-sure halting (Huber, 2015).

Its most prominent feature is the small-p1p_18 cost profile. Let p1p_19 and p2p_20. For p2p_21, the source states that

p2p_22

In particular,

p2p_23

so to first order one uses p2p_24 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 p2p_25 flips in the small-p2p_26 regime; on that basis, the two-coin linear factory is described as first-order optimal (Huber, 2015).

The large-p2p_27 regime is treated by imposing a known cutoff p2p_28. Under that assumption, the expected cost is stated to be p2p_29, improving substantially on older f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,0 factories. The same implementation notes recommend that, in practice, once f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,1 becomes say f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,2, one may switch to the classical Nacu–Peres factory for functions of the form f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,3; the resulting hybrid is stated to use at most approximately f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,4 flips when f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,5 (Huber, 2015).

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

f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,6

where f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,7 are known constants and f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,8 are unknown probabilities from which Bernoulli draws can be simulated (Gonçalves et al., 2017).

The algorithm is iterative and exceptionally simple. In each loop, one first flips

f(p1,p2)=C1p1+C2p2,f(p_1,p_2)=C_1p_1+C_2p_2,9

If r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.0, one flips a Bernoullir=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.1 coin and returns r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.2 if that coin is r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.3; otherwise the loop continues. If r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.4, one flips a Bernoullir=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.5 coin and returns r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.6 if that coin is r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.7; otherwise the loop continues. The source formulates correctness by defining the one-iteration output probabilities

r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.8

with repeat probability r=C1p1+C2p2M<1.r=C_1p_1+C_2p_2\le M<1.9. Summing the geometric series gives

p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},0

and similarly

p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},1

Hence the returned variable is exactly p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},2 (Gonçalves et al., 2017).

The expected cost is also explicit. The termination probability per loop is

p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},3

so the number of loops is geometric with mean

p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},4

Since each loop uses exactly one “p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},5-coin” plus one “p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},6-coin,” the expected total number of coin flips is

p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},7

The source notes that the cost grows as p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},8 when p=c1p1c1p1+c2p2,p=\frac{c_1p_1}{c_1p_1+c_2p_2},9 or C1p1+C2p2C_1p_1+C_2p_200 when C1p1+C2p2C_1p_1+C_2p_201 (Gonçalves et al., 2017).

In Bayesian computation, this ratio form is used to realize Barker’s acceptance rule for intractable target densities. With

C1p1+C2p2C_1p_1+C_2p_202

the Barker acceptance probability can be written as

C1p1+C2p2C_1p_1+C_2p_203

where C1p1+C2p2C_1p_1+C_2p_204 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 C1p1+C2p2C_1p_1+C_2p_205, and that the asymptotic variance of Barker is at worst twice that of Metropolis–Hastings, plus a C1p1+C2p2C_1p_1+C_2p_206 term (Gonçalves et al., 2017).

5. General two-input theory

The multiparameter theory of Bernoulli factories places the two-coin problem in a complete characterization framework. For C1p1+C2p2C_1p_1+C_2p_207, a Bernoulli factory that terminates almost surely with finite expectation everywhere exists if and only if two conditions hold: first, C1p1+C2p2C_1p_1+C_2p_208 is continuous on C1p1+C2p2C_1p_1+C_2p_209; second, both C1p1+C2p2C_1p_1+C_2p_210 and C1p1+C2p2C_1p_1+C_2p_211 are polynomially bounded on every face of the square in the sense specified by the boundary monomials associated with the face decomposition (Leme et al., 2022).

The source summarizes this criterion in words: on each face where C1p1+C2p2C_1p_1+C_2p_212 does not vanish identically, it can never dip below a fixed constant times the corresponding boundary-monomial to some power, and likewise for C1p1+C2p2C_1p_1+C_2p_213. It states that this exactly generalizes the one-coin Keane–O’Brien condition

C1p1+C2p2C_1p_1+C_2p_214

This result is a structural characterization rather than a special-purpose construction for a single target (Leme et al., 2022).

For implementable C1p1+C2p2C_1p_1+C_2p_215, the source gives a general two-coin factory based on “recursive splitting plus Boolean test.” One precomputes a threshold-test sample size C1p1+C2p2C_1p_1+C_2p_216 and a helper-coin probability C1p1+C2p2C_1p_1+C_2p_217. A finite-depth subroutine C1p1+C2p2C_1p_1+C_2p_218 flips each input coin exactly C1p1+C2p2C_1p_1+C_2p_219 times, forms C1p1+C2p2C_1p_1+C_2p_220, computes the deterministic predicate

C1p1+C2p2C_1p_1+C_2p_221

and outputs C1p1+C2p2C_1p_1+C_2p_222. The full factory then uses a geometric helper-coin mechanism with parameter C1p1+C2p2C_1p_1+C_2p_223 to mix calls to C1p1+C2p2C_1p_1+C_2p_224. The source states that the expected number of C1p1+C2p2C_1p_1+C_2p_225-calls is C1p1+C2p2C_1p_1+C_2p_226, each call to C1p1+C2p2C_1p_1+C_2p_227 uses C1p1+C2p2C_1p_1+C_2p_228 flips of the unknown coins, and therefore the expected total number of flips is C1p1+C2p2C_1p_1+C_2p_229 (Leme et al., 2022).

The same exposition gives two illustrative examples. For

C1p1+C2p2C_1p_1+C_2p_230

the direct factory is simply: flip coinC1p1+C2p2C_1p_1+C_2p_231 once; if C1p1+C2p2C_1p_1+C_2p_232 then flip coinC1p1+C2p2C_1p_1+C_2p_233 once and output that result; otherwise output C1p1+C2p2C_1p_1+C_2p_234. The source states that the complexity is at most C1p1+C2p2C_1p_1+C_2p_235 flips. It also discusses

C1p1+C2p2C_1p_1+C_2p_236

on the domain C1p1+C2p2C_1p_1+C_2p_237 as a further worked example (Leme et al., 2022).

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 C1p1+C2p2C_1p_1+C_2p_238-approximates any target distribution C1p1+C2p2C_1p_1+C_2p_239 and an unbounded-length, almost-surely finite procedure that implements C1p1+C2p2C_1p_1+C_2p_240 exactly. It states that the fixed-length version uses C1p1+C2p2C_1p_1+C_2p_241 flips per coin, while the unbounded exact version uses expected C1p1+C2p2C_1p_1+C_2p_242 flips per coin with

C1p1+C2p2C_1p_1+C_2p_243

and that both schemes terminate with probability C1p1+C2p2C_1p_1+C_2p_244 even if one coin is fully controlled (Solan et al., 2018).

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 (Patel et al., 2018).

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 (Huber, 2015, Gonçalves et al., 2017, Patel et al., 2018).

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Two-Coin Bernoulli Factory Algorithm.