<p>Modulo reduction is a commonly seen way to make a random integer generator avoid the worst case of running forever.</p>
<p>When the range of possible integers is unknown, however, there is no way in general to &quot;fix&quot; this worst case of running forever without introducing bias. It's not just modulo reduction (<code>rand() % n</code>, discussed in the accepted answer) that will introduce bias this way, but also the &quot;multiply-and-shift&quot; reduction of Daniel Lemire, or if you stop rejecting an outcome after a set number of iterations.  (To be clear, this doesn't mean there is no way to fix the bias issues present in pseudorandom generators.  For example, even though modulo and other reductions are biased in general, they will have no issues with bias if the range of possible integers is a power of 2 <em>and</em> if the random generator produces unbiased random bits or blocks of them.)</p>
<p>The rest of this answer will show the relationship between running time and bias in random generators.  From here on, we will assume we have a &quot;true&quot; random generator that can produce unbiased and independent random bits.*</p>
<p>In 1976, D. E. Knuth and A. C. Yao showed that any algorithm that produces random integers with a given probability, using only random bits, can be represented as a binary tree, where random bits indicate which way to traverse the tree and each leaf (endpoint) corresponds to an outcome. In this case, we're dealing with algorithms that generate random integers in [0, n), where each integer is chosen with probability 1/n. The algorithm is <em>unbiased</em> if the same number of leaves appear in the tree for all outcomes.  But if 1/n has a non-terminating binary expansion (which will be the case if n is not a power of 2), the algorithm will be unbiased only if—</p>
<ul>
<li>the binary tree has an &quot;infinite&quot; depth, or</li>
<li>the binary tree includes &quot;rejection&quot; leaves at the end,</li>
</ul>
<p>and in either case, the algorithm won't run in constant time and will run forever in the worst case. (On the other hand, when <code>n</code> is a power of 2, the optimal binary tree will have a finite depth and no rejection nodes.)</p>
<p>The binary tree concept also shows that any way to &quot;fix&quot; this worst-case time complexity will lead to bias in general. (Again, this doesn't mean there is no way to fix the bias issues present in pseudorandom generators.)  For instance, modulo reductions are equivalent to a binary tree in which rejection leaves are replaced with labeled outcomes — but since there are more possible outcomes than rejection leaves, only some of the outcomes can take the place of the rejection leaves, introducing bias. The same kind of binary tree — and the same kind of bias — results if you stop rejecting after a set number of iterations. (However, this bias may be negligible depending on the application. There are also security aspects to random integer generation, which are too complicated to discuss in this answer.)</p>
<p>To illustrate, the following JavaScript code implements a random integer algorithm called the <a href="https://arxiv.org/abs/1304.1916" rel="nofollow noreferrer">Fast Dice Roller</a> by J. Lumbroso (2013). Note that it includes a rejection event and a loop which are necessary to make the algorithm unbiased in the general case.</p>
<pre><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 ? 0 : 1)
    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>
<h3>Note</h3>
<p>* This answer won't involve the <code>rand()</code> function in C because it <a href="https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad/52881465#52881465">has many issues</a>. Perhaps the most serious here is the fact that the C standard does not explicitly specify a particular distribution for the numbers returned by <code>rand()</code>, not even a uniform distribution.</p>
Why do people say there is modulo bias when using a random number generator?
<p>I have seen this question asked a lot but never seen a true concrete answer to it. So I am going to post one here which will hopefully help people understand why exactly there is "modulo bias" when using a random number generator, like <code>rand()</code> in C++.</p>