<p>This problem can be restated as:</p>

<ul>
<li>Generate a random integer in the interval [0, N).</li>
<li>Output 1 if the integer is 0, or 0 otherwise.</li>
</ul>

<p>There are various ways to <a href="https://peteroupc.github.io/randomfunc.html#Uniform_Random_Integers" rel="nofollow noreferrer">generate random integers</a> in a range from a random bit stream. Of these, J. Lumbroso showed an optimal way to solve this problem given a random bit stream ("<a href="http://arxiv.org/abs/1304.1916" rel="nofollow noreferrer">Optimal Discrete Uniform Generation from Coin Flips, and Applications</a>", 2013). (However, Appendix B of that paper also points out a solution to your direct problem: generating 0 or 1 with a given probability.) Other ways include those mentioned in "<a href="http://www.pcg-random.org/posts/bounded-rands.html" rel="nofollow noreferrer">Efficiently Generating a Random Number in a Range</a>" as well as a brand-new algorithm, the "<a href="https://github.com/probcomp/fast-loaded-dice-roller" rel="nofollow noreferrer">Fast Loaded Dice Roller</a>".</p>
Uniformly distributed bit sequence
<p>Suppose you have a regular number generator which is able to produce uniformly distributed random 32 bit numbers. And suppose you look for a way to generate a pseudo-random sequence of bits where ones (i.e., the set bits) appear in the sequence with predefined probability.</p>

<p>A naive way of producing such sequence would be running number generator on per bit level but it's terribly inefficient for small probabilities like 0.01 or 1% of bits in the sequence most of the bits will be zero. On average just one bit in a hundred would be set. On the other hand even with such low probability there's a chance to encounter a long sub-sequence of consecutive ones that extends beyond the 8, 16, 32, 64 bits.</p>

<p>The question is how to produce such sequence efficiently using regular PRNG.</p>

<hr>

<p><strong>Edit</strong></p>

<p>A toy implementation of rational Bernoulli variable sampling in javascript suggested by Peter O.:</p>

<pre><code>// Based on
// https://arxiv.org/abs/1304.1916
// https://arxiv.org/pdf/1304.1916.pdf (page 21, figure 6)

class Xor128 {
    constructor(x, y, z, w) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
    }

    prev() {
        var t = this.w ^ this.z ^ (this.z &gt;&gt;&gt; 19);

        t ^= t &gt;&gt;&gt; 8;
        t ^= t &gt;&gt;&gt; 16;

        this.w = this.z;
        this.z = this.y;
        this.y = this.x;

        t ^= t &lt;&lt; 11;
        t ^= t &lt;&lt; 22;

        this.x = t;

        return this.w;
    }

    curr() {
        return this.w;
    }

    next() {
        var t = this.x ^ (this.x &lt;&lt; 11);

        this.x = this.y;
        this.y = this.z;
        this.z = this.w;

        return this.w = this.w ^ (this.w &gt;&gt;&gt; 19) ^ (t ^ (t &gt;&gt;&gt; 8));
    }
}

function* flip(xor128) {
    while (true) {
        var value =  xor128.next();

        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
        yield value &amp; 1; value &gt;&gt;&gt;= 1;
    }
} 

function* bernoulli(flip, k, n) {
    var b;
    var v = k

    for (const bit of flip) {
        v &lt;&lt;= 1;

        if (v &gt;= n) {
            v -= n;

            b = 1;
        } else {
            b = 0;
        }

        if (bit === 1) {
            yield b;

            v = k;
        }
    }
}

var xor128 = new Xor128(1, 2, 3, 4);

var z = 0, o = 0;

var then = Date.now();

for (const value of bernoulli(flip(xor128), 5, 1000)) {
    if (value === 0) {
        z++;
    } else {
        o++;
    }

    if (Date.now() - then &gt; 1000) {
        console.log(`${z} ${o}`);
    }
}


// Pieces of code to test out xor128:
//
// for (let index = 0; index &lt; 100; index++) {
//     console.log(xor128.curr())

//     xor128.next();
// }

// console.log('-----------------------------------')
// for (let index = 0; index &lt; 100; index++) {
//     xor128.prev();

//     console.log(xor128.curr())
// }
</code></pre>

<p><strong>Another edit</strong></p>

<p>The code below is implemented in C# produces 91.2 million bits per second packed into UInt32 data type (MacBookPro 2019 Core I9 2.4 Ghz). I think in C it'd be possible to get over 100 million bits, also it feels like it is possible to further utilize binary arithmetic to generate all 32 bits of random number in parallel, some loop unrolling or maybe SIMD not sure, anyway here's the code: </p>

<pre><code>public class Bernoulli
{
    public UInt32 X { get; set; }
    public UInt32 Y { get; set; }
    public UInt32 Z { get; set; }
    public UInt32 W { get; set; }

    public Bernoulli()
        : this(Guid.NewGuid())
    {

    }

    public Bernoulli(Guid guid)
    {
        var index = 0;
        var bytes = guid.ToByteArray();

        X = (UInt32)((bytes[index++] &lt;&lt; 24) | (bytes[index++] &lt;&lt; 16) | (bytes[index++] &lt;&lt; 8) | bytes[index++]);
        Y = (UInt32)((bytes[index++] &lt;&lt; 24) | (bytes[index++] &lt;&lt; 16) | (bytes[index++] &lt;&lt; 8) | bytes[index++]);
        Z = (UInt32)((bytes[index++] &lt;&lt; 24) | (bytes[index++] &lt;&lt; 16) | (bytes[index++] &lt;&lt; 8) | bytes[index++]);
        W = (UInt32)((bytes[index++] &lt;&lt; 24) | (bytes[index++] &lt;&lt; 16) | (bytes[index++] &lt;&lt; 8) | bytes[index++]);
    }

    public Bernoulli(UInt32 x, UInt32 y, UInt32 z, UInt32 w)
    {
        X = x;
        Y = y;
        Z = z;
        W = w;
    }


    UInt64 bits = 0;
    UInt32 bitsCount = 0;

    public UInt32 Next(UInt32 k, UInt32 n)
    {
        UInt32 b;
        var c = 0;
        var v = k;
        var r = 0u;

        // ------------------------

        do
        {
            while (bitsCount &lt;= 32)
            {
                b = X ^ (X &lt;&lt; 11);

                X = Y;
                Y = Z;
                Z = W;

                bits &lt;&lt;= 32;
                bits |= ((UInt64)(W = W ^ (W &gt;&gt; 19) ^ (b ^ (b &gt;&gt; 8))));
                bitsCount += 32;
            }

            while (c &lt; 32 &amp;&amp; 0 &lt; bitsCount)
            {
                v &lt;&lt;= 1;

                // Two lines of code below is a two step optimization:
                // First we optimize the following statement:
                //
                // if (v &gt;= n)
                // {
                //     v -= n;
                //     b = 1;
                // }
                // else
                // {
                //     b = 0;
                // }
                //
                // into the following:
                //
                // var b = v &lt; n ? 0u : 1u;
                // v -= b * n
                //
                // thus reducing branching, but we would like also to omit
                // multiplication, which we can do through:
                b = v &lt; n ? 0u : 0xFFFFFFFFu;
                v -= b &amp; n;

                if ((bits &amp; 1) == 1)
                {
                    r |= b &amp; 1;
                    r &lt;&lt;= 1;
                    v = k;
                    c++;
                }

                bits &gt;&gt;= 1;
                bitsCount--;
            }

        } while (c &lt; 32);

        return r;
    }
}
</code></pre>