(Summarizing the discussion from the comments with added quantitative insights)
Within the given constraints, achieving both - an exactly uniform distribution as well as deterministically finite runtime, is impossible. We can tradeoff one for the other.
Method 1- Rejection Sampling
This method achieves exactly uniform distribution but does not have a deterministic runtime. The idea is, generate $n \in [0, 255]$. If $n \geq 210$, repeat the process; otherwise return the value.
Let $T$ be the number of trials, then the distribution follows: $$\Pr[T>k]=\left(1−\frac{210}{256}\right)^k $$
This gives, $\mathbb{E}[T] \approx 1.22$, and $\Pr[T > 5] \approx 0.02\% $ which is extremely low.
$\Pr[T > 120]$ is much lesser than $1 \over {\text{# atoms in the universe}}$
Method 2- Multiply-and-floor
Each trial of the rng gives 1 byte. Draw
$k$ independent bytes, and concat to get the integer $X$. Then, consider
$$ m = \left\lfloor X \times \dfrac{210}{2^{8k}} \right\rfloor \in [0, 209] $$
This runs in exactly
$k$ draws, but is slightly biased because
$2^{8k}$ is never a multiple of 210.
Let's quantify the bias.
Exactly $ R_k = 2^{8k}\bmod 210$ of the 210 output-values get a slightly higher probability. Let's call this set $Q_k$. We have, $$
\Pr[m=i] =
\begin{cases}
\dfrac{\lfloor2^{8k}/210\rfloor+1}{2^{8k}} & i\in Q_k \\
\dfrac{\lfloor2^{8k}/210\rfloor}{2^{8k}} & \text{otherwise}.
\end{cases}
$$
Maximum bias, from the expected $1/210$ is then approx $2^{-8k}$.
With just $k=1$, the bias is already less than $0.5\%$.
- Using random hexadecimal characters to generate an even distribution of random numbers within an arbitrary base-10 range,
- A way to roll ANY number between 1 and x using standard dice,
- Use any dice to calculate the outcome of any probability,
- How to use small random integer generator to make a big one.
– David K May 25 '25 at 20:26- Expanding the precision or range of a random number generator
- Generating a number using a coin
- Generate a number with a die that has three 0s and three 1s
- Is it possible to create a completely random integer between 1 and 13 using standard dice in a D&D dice kit
– David K May 25 '25 at 20:29- How to generate a random number between 1 and 10 with a six-sided die
- How to use dice to randomly select from 10 choices?
- Simplest way to produce an even distribution of random values?
- Best way to generate U(1,5) from U(1,3)
– David K May 25 '25 at 20:30