Transform One Discrete Uniform Distribution to Another On a job interview I was asked the following question:
Assume that you have a function in some programming language called
$$\text{randint}_{32,000}()$$
Which produces a random integer in the interval $[0, 31999]$. The integer is chosen according to a uniform distribution.
The exercise is to use this function to program a new function,
$$\text{randint}_{1,000,000}()$$
which will produce random integers in the interval $[0, 999999]$, so that their distribution is going to be again uniform.
My first thought was a linear map from $[0, 31999]$ to $[0, 999999]$ but this is incorrect as this linear map is invertible, and only $32000$ integers from $[0, 999999]$ would have been a possible result of $\text{randint}_{1,000,000}()$.
Another thing I thought is that such transformation could have been possible if the source function was $\text{randint}_{1,000}()$. Then we would have first chosen the index of a thousand to look in, and then the index within that thousand. In other words, if $X,Y \sim U[0,999]$, $X,Y$ independent, then $1000X + Y \sim U[0,999999]$.
Is there a solution for the original problem?
 A: As a hint, I can generate a random number from $1$ to $12$ uniformly at random by rolling a six-sided die twice.  The first result I multiply by $2$ and I add this to the second result mod $2$, so $(5,2)$ results in $10$ while $(1,1)$ results in $1$, etc...  You should be able to do something similar here, the numbers will just be larger.  That is, 2(rand6() + 1) - rand6()%2
The table for my dice example, rows correspond to first die's result and columns to the second die's result:
$\begin{array}{c|cccccc}&1&2&3&4&5&6\\\hline1&1&2&1&2&1&2\\2&3&4&3&4&3&4\\3&5&6&5&6&5&6\\4&7&8&7&8&7&8\\5&9&10&9&10&9&10\\6&11&12&11&12&11&12\end{array}$
I am in effect using each roll to narrow down the possibilities of the final result each time, using the first result to decide which "zone" to end in and the second result to more provide more fine of detail.
It is worth noting that the primes in the expansion of $12$ are the same as the primes in the expansion of $6$.
It is also worth noting that the primes in the expansion of $1000000=2^6\times 5^6$ are the same as the primes in the expansion of $32000=2^8\times 5^3$.  This is crucial so that we have a uniform distribution.
You could theoretically use multiple iterations of a randX method to simulate a randY method for any $X$ and $Y$ so long as all prime divisors of $Y$ are also prime divisors of $X$, i.e. some integer $n$ such that $Y\mid X^n$.

 (125 * ((randint32000() % 8000)+1) - (randint32000() % 125))-1.  The addition and subtraction by one is a result of us wanting to generate a number in the range which began at zero rather than the range that ended at 32000 or 1000000

A: @JMoravitz gave a great full answer.  But to answer your second half directly, observe that $32000$ is a multiple of $1000$, so 
randint1000() = randint32000() / 32 (integer division, drop all fractions)
   or
randint1000() = randint32000() % 1000 (mod 1000)

