I'm afraid I don't speak maths very well. I hope this question is sufficiently comprehensible and mathematical.

Suppose I have a perfect D$x$ (i.e. $x$-sided) die, and a pen and paper, and with these I wish to obtain a random integer between 1 and $n$ such that every possible result $r$ is equally likely, where $x≥2$, $n>1$, and $1≤r≤n$.

Is there a known algorithm/procedure/function/protocol that provides such output from such input? If so, what is it called, and how does it work?

(Presumably, the protocol will involve rolling the die more than once if $n>x$.)

  • $\begingroup$ search term for you, arithmetic encoding. $\endgroup$ – adam W Mar 22 '15 at 6:28

There are many such algorithms, with different efficiencies. There is a very simple algorithm which I will demonstrate with two examples.

First, consider the case where you want to use a six sided dice to generate a random number between 1 and 100.

Roll the dice 3 times. There are 216 (6 x 6 x 6) possible results. If you roll 1,1,1 the random number is 1. If it is 1,1,2 the random number is 2, ... 1,1,6 gives the random number 6, 1,2,1 is 7 ... 1,2,6 is 12 ... 3,3,1 is 100. If it is higher than 3,3,1 then discard the answer and throw the dice another 3 times.

This is not particularly efficient, because you will have to roll the dice on average about 6 times (216/100 * 3) to produce one random number. But it is easy to generate.

Or if your dice is 2 sided (like a coin), and you want a number between 1 and 1,000, flip it 10 times. Considered as a binary number where Heads = 1 and Tails = 0 this will give you a random number between 0 and 1023. If you only want numbers between 1 and 1,000 inclusive, then if your ten throws produces 0 or a number between 1001 and 1023 then start again. (this actually is reasonably efficient, because you will only have to repeat the experiment for a 24/1024 possible sequences).

The same idea can be used for any number of sides and any possible range of random numbers.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.