<p>The answer is that it is impossible in general to generate a random unbiased integer in [0, <code>n</code>) in constant time. One notable exception is when the source of random numbers produces unbiased random bits and <code>n</code> is a power of 2.</p>
<p>For instance, assume we have a &quot;true&quot; random generator and can produce unbiased random bits. Then, unless <code>n</code> is a power of 2, there are only two possible ways to proceed:</p>
<ul>
<li>It can use modulo reduction (or Lemire's multiply-then-shift reduction). This will run in constant time, but introduce a bias (some numbers are slightly more likely to be generated than others).</li>
<li>It can use rejection sampling. This will introduce no bias, but can run forever in the worst case (even though it has an expected constant time complexity). Many kinds of algorithms fit in this category, including modulo reduction followed by a rejection step (which is necessary if <code>n</code> is not a power of 2), as well as the <a href="https://arxiv.org/abs/1304.1916" rel="nofollow noreferrer">Fast Dice Roller</a> (which uses random bits).</li>
</ul>
<p>(See my note on <a href="https://peteroupc.github.io/randomfunc.html#RNDINT_Random_Integers_in_0_N" rel="nofollow noreferrer">integer generating algorithms</a> for a survey of both kinds of algorithms. For a Fast Dice Roller implementation, see <a href="https://stackoverflow.com/a/62920514/815724">another answer of mine</a>.)</p>
<p>In this sense, Knuth and Yao showed in 1976 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. (Knuth and Yao, &quot;The complexity of nonuniform random number generation&quot;, in Algorithms and Complexity, 1976.) In this case, each integer in [0, n) can occur with probability 1/n. And if 1/n has a non-terminating binary expansion (which will be the case if <code>n</code> is not a power of 2), this binary tree will necessarily either—</p>
<ul>
<li>have an &quot;infinite&quot; depth, or</li>
<li>include &quot;rejection&quot; leaves at the end of the tree,</li>
</ul>
<p>And in either case, the algorithm won't run in constant time.</p>
<p>Modulo or similar 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. (See also chapter 15 of <em>Non-Uniform Random Variate Generation</em> by L. Devroye, 1986.)</p>
<p><strong>Therefore:</strong> In general, an integer generator can be <em>either</em> unbiased <em>or</em> constant-time, but not both.</p>
<p>If you can't tolerate the worst case of running forever, then the only thing you can do is set a fixed maximum number of rejections or use a reduction, both of which can introduce bias. However, this bias might be negligible depending on your application (e.g., if the chance the algorithm &quot;fails&quot; is negligible compared to the chance it &quot;succeeds&quot;, for the application's purposes). There are also security aspects to random integer generation, which are too complicated to discuss in this answer.</p>
How to generate an unbiased random bigint in constant time
<p>In my embedded project, I have a biginteger class that handles arbitrary length integers.  I would like to be able to generate a random bigint between 0 and an arbitrary number.  Assume I have a quality source of random bytes.</p>

<p>All the implementations I have seen essentially do the same thing:  </p>

<ol>
<li>Generate a big number with the correct number of bytes,</li>
<li>If it is greater than  max, generate again.</li>
</ol>

<p>The problem I see with this implementation is that it could take an awfully long time.  Imagine that <code>max = 2^2049-1</code>  =(<code>01 FF .. FF</code>).  This algorithm will generate 257 bytes, then check that the most significant byte is <code>&lt;=1</code>.  So there is a 254/256 chance it has to generate a whole new 257 byte number.  In the (admittedly unlikely) worst case, this loop could go on for minutes or years.</p>

<p>My question is:<br>
In the case where the generated number is too large,  is there a way to keep most of the bytes I have already generated?<br>
Is it valid to just regenerate the most significant byte, or does that introduce bias?  What about shifting the result right one digit? </p>

<p>Is there any way to make the time deterministic, while still avoiding bias?</p>

<p>--</p>

<p>Another edge case:  <code>max = 2^2048 + 1</code> = (<code>01 00 .. 01</code>)  In this case the most significant byte can be non-zero if the remaining bytes are 0s followed by a <code>00</code> or <code>01</code>.  So most of the time, if the MSB is non-zero, than it will be invalid, and just regenerating that byte will never make it valid.  But just force setting it to zero seems wrong also.</p>