3

I am given a uniform integer random number generator $\sim U_3(1,3)$ (inclusive). I would like to generate integers $\sim U_5(1,5)$ (inclusive) using $U_3$. What is the best way to do this?

This simplest approach I can think of is to sample twice from $U_3$ and then use rejection sampling. i.e., sampling twice from $U_3$ gives us 9 possible combinations. We can assign the first 5 combinations to 1,2,3,4,5, and reject the last 4 combinations.

This approach expects to sample from $U_3$ $\frac{9}{5} * 2 = 18/5 = 3.6$ times.

Another approach could be to sample three times from $U_3$. This gives us a sample space of $27$ possible combinations. We can make use of $25$ of these combinations and reject the last 2. This approach expects to use $U_3$ $\frac{27}{25} * 3.24$ times. But this approach would be a little more tedious to write out since we have a lot more combinations than the first, but the expected number of sampling from $U_3$ is better than the first.

Are there other, perhaps better, approaches to doing this?

  • 1
    Are you sampling integers or real numbers? – Henry Sep 06 '20 at 23:20
  • @Henry integers. I'll include that int he OP – roulette01 Sep 06 '20 at 23:20
  • Possible duplicate of https://math.stackexchange.com/questions/1314460/how-to-generate-a-random-number-between-1-and-10-with-a-six-sided-die – ypercubeᵀᴹ Sep 07 '20 at 17:15
  • @ypercubeᵀᴹ There's many variants of this problem, but with different bases (perhaps distinct set of bases may have unique solutions that do not apply generlaly), so it's not necessarily a duplicate. – roulette01 Sep 07 '20 at 17:20
  • In short, consider the output of dice U3 as u1, u2, u3, ... (using 01,2, not 1-3!) as the digits of the number (in base 3): a = 0.u1u2u3... Then convert this number to base 5: 0.v1v2v3.... The numbers v1, v2, v3, ... (which are 0-4 since it's base 5) is your U5 – ypercubeᵀᴹ Sep 07 '20 at 17:21

2 Answers2

1

Your calculations are correct. The more efficient rejection sampling scheme is to generate three $U_3$ variables, say $U_{3,0}, U_{3,1}, U_{3,2}$ and compute $$S_3 = \sum_{i=0}^2 3^i (U_{3,i} - 1).$$ If $S_3 > 25$, which happens with probability $2/27$, we generate another set (i.e., reject); otherwise, we compute $\lceil S_3/5\rceil$ and this will be distributed as $U_5$. I don't think this is too computationally expensive to implement compared to the two-variable case. For instance, if you generate the sample $$(3,1,2),$$ then you can compute $$(3,1,2) \cdot (9,3,1) - 13 = 19,$$ or $$(2,0,1) \cdot (9,3,1) = 19;$$ both are equivalent. Then since $19 \le 25$, we compute $\lceil 19/5 \rceil = 4$.

heropup
  • 135,869
  • Wait, is your method the same one as the second method I proposed? It seems to be? – roulette01 Sep 07 '20 at 02:17
  • @roulette01 Yes, it is the same, just formalized mathematically. – heropup Sep 07 '20 at 03:08
  • K, just making sure I wasn't missing something. For these kind of problems, I always wonder if there's another approach that, perhaps, look at the sum of products of the game sample space of numbers instead of mapping pairs/triplets to another uniform distribution. – roulette01 Sep 07 '20 at 03:41
1

You approach is on the rigth track.

Notice that in your second proposal you need (in average) 3.24/2 = 1.62 values of $U_3$ to generate one values of $U_5$.

This is already quite near the theorical limit, which is $\log(5)/\log(3) = 1.465$

To attain this bound (with much more complexity of coding) you could use arithmetic coding.

leonbloy
  • 63,430