<h2>If you have coins with a <em>known</em> probability of heads</h2>
<p>Assume you have a function <code>unfairCoin(p)</code>, which is a function that produces heads with a <em>known</em> probability <code>p</code> and tails otherwise. For example, it could be implemented like this:</p>
<pre><code>function unfairCoin(p) {
   return Math.random() &lt; p ? True : false;
}
</code></pre>
<p>Here is an algorithm that solves your problem given <code>unfairCoin</code>, assuming all the probabilities involved sum to 1:</p>
<ol>
<li>Set <code>cumu</code> to 1.</li>
<li>For each item starting with the first:
<ol>
<li>Get the probability associated with the chosen item (call it <code>p</code>) and accept the item with probability <code>p / cumu</code> (e.g., via <code>unfairCoin(p / cumu)</code>).  If the item is accepted, return that item.</li>
<li>If the item was not accepted, subtract <code>p</code> from <code>cumu</code>.</li>
</ol>
</li>
</ol>
<p>This algorithm's expected time complexity depends on the order of the probabilities. In general, the algorithm's time complexity is linear, but if the probabilities are sorted in descending order, the expected time complexity is constant.</p>
<p>EDIT (Jul. 30): As I've just found out, this exact algorithm was already described by Keith Schwarz in <a href="https://www.keithschwarz.com/darts-dice-coins/" rel="nofollow noreferrer">Darts, Dice, and Coins</a>, in &quot;Simulating a Loaded Die with a Biased Coin&quot;. That page also contains a proof of its correctness.</p>
<hr />
<p>An alternative solution uses rejection sampling, but requires generating a random integer using fair coin tosses:</p>
<ol>
<li>Generate a uniform random integer index in the interval [0, n), where <code>n</code> is the number of items. This can be done, for example, using the <a href="https://arxiv.org/abs/1304.1916" rel="nofollow noreferrer">Fast Dice Roller</a> by J. Lumbroso, which uses only fair coin tosses (<code>unfairCoin(0.5)</code>); see the code below. Choose the item at the given index (starting at 0).</li>
<li>Get the probability associated with the chosen item (call it <code>p</code>) and accept it with probability <code>p</code> (e.g., via <code>unfairCoin(p)</code>). If the item is accepted, return that item; otherwise, go to step 1.</li>
</ol>
<p>This algorithm's expected time complexity depends on the difference between the lowest and highest probability.</p>
<p>Given the weights for each item, there are many other ways to make a weighted choice besides the algorithms given earlier; see my <a href="https://peteroupc.github.io/randomfunc.html#Weighted_Choice_With_Replacement" rel="nofollow noreferrer">note on weighted choice algorithms</a>.</p>
<h3>Fast Dice Roller Implementation</h3>
<p>The following is JavaScript code that implements the Fast Dice Roller. Note that it uses a rejection event and a loop to ensure it's unbiased.</p>
<pre class="lang-js prettyprint-override"><code>function randomInt(minInclusive, maxExclusive) {
 var maxInclusive = (maxExclusive - minInclusive) - 1
 var x = 1
 var y = 0
 while(true) {
    x = x * 2
    var randomBit = Math.random()&lt;0.5 ? 1 : 0
    y = y * 2 + randomBit
    if(x &gt; maxInclusive) {
      if (y &lt;= maxInclusive) { return y + minInclusive }
      // Rejection
      x = x - maxInclusive - 1
      y = y - maxInclusive - 1
    }
 }
}
</code></pre>
<p>The following version returns a BigInt, an arbitrary-precision integer supported in recent versions of JavaScript:</p>
<pre class="lang-js prettyprint-override"><code>function randomInt(minInclusive, maxExclusive) {
 minInclusive=BigInt(minInclusive)
 maxExclusive=BigInt(maxExclusive)
 var maxInclusive = (maxExclusive - minInclusive) - BigInt(1)
 var x = BigInt(1)
 var y = BigInt(0)
 while(true) {
    x = x * BigInt(2)
    var randomBit = BigInt(Math.random()&lt;0.5 ? 1 : 0)
    y = y * BigInt(2) + randomBit
    if(x &gt; maxInclusive) {
      if (y &lt;= maxInclusive) { return y + minInclusive }
      // Rejection
      x = x - maxInclusive - BigInt(1)
      y = y - maxInclusive - BigInt(1)
    }
 }
}
</code></pre>
<h2>If you have coins with an <em>unknown</em> probability of heads</h2>
<p>If on the other hand, you have a function <code>COIN</code> that outputs heads with an <em>unknown</em> probability and tails otherwise, then there are two problems to solve to get to the solution:</p>
<ol>
<li>How to turn a biased coin into a fair coin.</li>
<li>How to turn a fair coin into a loaded die.</li>
</ol>
<p>In other words, the task is to turn a biased coin into a loaded die.</p>
<p>Let's see how these two problems can be solved.</p>
<h3>From biased to fair coins</h3>
<p>Assume you have a function <code>COIN()</code> that outputs heads with an unknown probability and tails otherwise. (If the coin is <em>known</em> to have probability 0.5 of producing heads then you already have a fair coin and can skip this step.)</p>
<p>Here we can use von Neumann's algorithm from 1951 of turning a biased coin into a fair coin. It works like this:</p>
<ol>
<li>Flip <code>COIN()</code> twice.</li>
<li>If both results are heads or both are tails, go to step 1.</li>
<li>If the first result is heads and the other is tails, take heads as the final result.</li>
<li>If the first result is tails and the other is heads, take tails as the final result.</li>
</ol>
<p>Now we have a fair coin <code>FAIRCOIN()</code>.</p>
<p>(Note that there are other ways of producing fair coins this way, collectively called <em>randomness extractors</em>, but the von Neumann method is perhaps the simplest.)</p>
<h3>From fair coins to loaded dice</h3>
<p>Now, the method to turn fair coins into loaded dice is much more complex. It suffices to say that there are many ways to solve this problem, and the newest of them is called the <a href="https://arxiv.org/abs/2003.03830" rel="nofollow noreferrer"><em>Fast Loaded Dice Roller</em></a>, which produces a loaded die using just fair coins (in fact, it uses on average up to 6 fair coin tosses more than the optimal amount to produce each loaded die roll). The algorithm is not exactly trivial to implement, but see my <a href="https://github.com/peteroupc/peteroupc.github.io/blob/master/randomgen.py" rel="nofollow noreferrer">Python implementation</a> and the <a href="https://github.com/probcomp/fast-loaded-dice-roller" rel="nofollow noreferrer">implementation</a> by the <em>Fast Loaded Dice Roller</em>'s authors.</p>
<p>Note that to use the Fast Loaded Dice Roller, you need to express each probability as a non-negative integer weight (such as 25, 40, 35 in your example).</p>
Can I achieve weighted randomness with a function that returns weighted booleans?
<p>I have a method that mimics an unfair coin. You can pass in a percentage, and it tells you whether or not you succeeded by returning a boolean. So if you call it with .25, it'll return <code>true</code> 25% of the time.</p>
<p>I'm trying to figure out if I can use this function to create a weighted randomness function that works like this: <code>There is a 25% chance it returns x, a 40% chance it returns y, and a 35% chance it returns z.</code> This is just an example. I would want the function to work for an unlimited amount of letters, but the percentages added together should equal 1.</p>
<p>The trick is, I want to be able to think about it the way I just described above. In other words:</p>
<pre><code>result = function ({.25, x}, {.4, y}, {.35, z})
</code></pre>
<p><code>result </code> should be x 25% of the time, and so on.  Can I implement this function with my unfairCoin?</p>
<hr />
<p>Here's how I worded it in a comment below.it might clarify what I'm asking for:</p>
<p>Correct my logic if I'm making a mistake here, but let's say XY and Z all had .3333... Couldn't I use my unfair coin to pass in .3333... If that returns true, that means you get X as a result. If it returns false, call my unfair again with .5 if that returns true, return Y, otherwise return Z. If that is correct, I don't know how to get this working if the numbers AREN'T .3333 and if there's more than three</p>