# Function (algorithm) for obtaining random number(s) from dice

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$.)

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