Given a fair $k$-sided die, with $k < n$, what is a generic method that allows me to generate a number in the range $[0...n]$ with the same probability and with the least amount of throws?
I have found many answer to similar specific questions, but I could not figure out a general method to achieve this.
It cannot be done unless every prime divisor of $n+1$ is a prime divisor of $k$.
That's because in $m$ tosses, there are $k^m$ possible outcomes, and an equal number of them must go to each of the $n+1$ values, $0,1,2,...,n$, which means that $k^m$ must be divisible by $n+1$. That's possible if and only if every prime divisor of $n+1$ is a divisor of $k$.
It is certainly possible to do it in $m$ tosses when $n+1$ is a divisor $k^m$. Take most intuitive algorithm is to take the $m$ numbers, $a_1,...,a_m$ from $0$ to $k-1$ and write them as a base $k$ number:
$$a_1+a_2k+a_3k^3+...+a_mk^{m-1}$$
then divide this number by $n+1$ and take the remainder.