5

Problem

I want to create an 'event' with probability of $\frac{1}{7}$ with a single dice as efficiently as possible (to roll the dice as little as possible).

To give you some better understanding of the question, if I would like an event with probability of $\frac{1}{9}$, I could easily do it in various ways. One way is to see whether the sum of two rolls is $5$. Another way is to roll the dice twice and see whether they are both not higher than $2$.

If I had to choose $\frac{1}{5}$, we can do this 'trick': success on drawing $1$, but if we draw $6$ just disregard it and roll again, until we draw a number which is not $6$. In that case the expected number of rolls is $\frac{6}{5}$.

So, one way to get probability of $\frac{1}{7}$ is basically the same, choosing $7$ predefined ordered pairs of 2 rolls to be the success set, and disregard any other combination (and to roll the dice twice again, if other combination occurs). In that case, the expected number of dice rolls needed is $\frac{72}{7}$ which seems pretty bad to me and makes we wonder... is there a better way?

Generalization

In general, if we have a $k$-sided dice what is the optimal way of obtaining an event with probability $\frac1m$ using the least expected dice rolls? And what if we want to simulate an $m$-side dice and not just one event?

d_e
  • 1,565

1 Answers1

8

Two rolls give $6 \times 6 = 5 \times 7 + 1$ possible outcomes. Just split them into $7$ equally likely groups except for one outcome and re-roll if you get that.

In general given a $k$-dice that you wish to use to simulate an $m$-dice, the algorithm is as follows: $\def\lfrac#1#2{{\large\frac{#1}{#2}}}$ $\def\floor#1{\lfloor #1 \rfloor}$

Set $n := 1$ and $x := 0$.   [$n$ is the number of branches; $x$ is the branch index (0-based).]

Repeat:

  Roll the $k$-dice once and let $p$ be the outcome.

  Set $n := kn$ and $x := kx+p$.   [Expand every branch and move down a random one.]

  If $n \ge m$ then:   [Enough to have $m$ equal parts.]

    Let $a,b$ be integers such that $n = am+b$ and $0 \le b < m$.   [$a$ is the size of each part.]

    If $x < am$ then: Return $x \bmod m$.   [Falls into one of the equal parts.]

    Otherwise: Set $n := b$ and $x := x-am$.   [Falls into the leftover branches.]

Clearly this algorithm $A$ halts with probability $1$. I also claim that it is optimal. Take any optimal algorithm using the $k$-dice. It will create a $k$-way outcome tree, with some branches ending in leaf nodes. Label each leaf with the output. Iteratively swap branches to make the leaves in decreasing order of weight. Within each level sort the leaves in order of label. Then [(1)] there are no $k$ leaves in a level with the same label otherwise they can be swapped to the front of that level and pushed up. I claim that this tree $T'$ is exactly the outcome tree $T$ for $A$. Suppose otherwise. Consider the first level where there is a difference. $T'$ must have less leaves than $T$ (because $A$ is greedy). But [by (1)] that extra leaf in $T$ has more weight than all smaller leaves with the same label in $T'$, and so it is impossible for $T'$ to make up the difference. Thus $T'$ is incorrect. Therefore the supposition is false and $T' = T$.

user21820
  • 57,693
  • 9
  • 98
  • 256
  • so simple...thanks. – d_e Jul 23 '16 at 17:17
  • right ..deleted this part of the comment ...so I guess I am wrong with $\frac{36}{7}$ in the post...is should be $\frac{72}{7}$ ? – d_e Jul 23 '16 at 17:20
  • I wonder if this generalizes nicely. – MT_ Jul 23 '16 at 17:20
  • @d_e: Ah yes I didn't notice that in your post itself. – user21820 Jul 23 '16 at 17:21
  • Of course the question was to do it in as few rolls as possible. Since $7$ does not divide $6^N$ there's no way to do it in $N$ rolls for any fixed $ N$. So the meaingful version of the question is to minimize the expected value of the number of rolls. I wonder what does that... – David C. Ullrich Jul 23 '16 at 17:23
  • @DavidC.Ullrich: That's what the re-roll is for. It's provably optimal to use up as many outcomes as possible at each roll. In general it means that we have to retain information about the outcome that we discard. In this case there is only one discarded outcome so it is optimal. – user21820 Jul 23 '16 at 17:26
  • @MichaelTong: See above comment. We need at least 2 rolls to get a subset of the outcome tree to have total probability less than $\frac17$, and we immediately take out as much as possible, so it cannot do worse than any other algorithm because all algorithms have to partition the same outcome tree. – user21820 Jul 23 '16 at 17:28
  • Could you please give an example of 7 equally likely groups? – John Bentin Jul 23 '16 at 17:43
  • @user21820 Let's say we had a $7$-sided die and want to achieve a probability of $1/19$. As you say, we need at least $2$ rolls because with $1$ roll only gets me a minimum probability of $1/7$. However, by generalization, I don't think it will be optimal here to partition the $49$ outcomes from $2$ rolls into a subset of $38$, taking $2$ outcomes as good, and then re-rolling the other $11$. Certainly I think it would be optimal to partition the $343$ outcomes from $3$ rolls into a subset of $342$ and re-rolling on the other $1$ outcome. – MT_ Jul 23 '16 at 17:44
  • @JohnBentin If $(6, 6)$ occurs then re-roll. Otherwise there are $35$ other outcomes, just arbitrarily partition those into $7$ equal sets of $5$. – MT_ Jul 23 '16 at 17:45
  • @user21820 That said, I haven't done the calculation explicitly, so it may still be more efficient in terms of expected value to do it the first way. – MT_ Jul 23 '16 at 17:47
  • @user21820 I'm certainly not disputing what you're saying here, but I don't see it. When you provably optimal, are you claiming that the proof is at least implicit in your comment? Say the whole tree is $6^\omega$, just to hint at what I mean by the notation in the following. Say we discarded $8$ outcomes after the first two rolls. We've lessened the chances of finishing in one roll, but how do we know there's not some other algorithm on $8\times 6^\omega$ that makes up for it somehow? – David C. Ullrich Jul 23 '16 at 17:49
  • @MichaelTong: With your $7$-sided dice, rerolling with probability $11/49$ beats rolling three times, for the expected number is $98/38$, which is less than $3$. – André Nicolas Jul 23 '16 at 17:55
  • @AndréNicolas Yeah I realized that the calculation doesn't work. I'm going to try and see if there's a better example where perhaps this doesn't work. – MT_ Jul 23 '16 at 17:59
  • @user21820 I thought for a second I had it, but I don't. Say we discarded $8$ outcomes after the first two rolls. Now $8\times 6>7$, so we can rig up something that gives a large probability of stopping in exactly $3$ rolls, while with the obvious algorithm if it takes more than two it must take at least four... – David C. Ullrich Jul 23 '16 at 18:03
  • Suppose we wanted to create a probability of $\frac{1}{6^{10}/2 + 1}$. Your reasoning says we roll $10$ times and $\frac{6^{10}/2 + 1}{6^{10}} = \frac{1}{2} + \frac{1}{6^{10}} := \frac{1}{2} + \varepsilon$ of the time we have an answer and $\frac{6^{10}/2 - 1}{6^{10}} = \frac{1}{2} - \varepsilon$ of the time we restart. So $E(X) = 10 (1/2 + \varepsilon) + (E(X) + 10) (1/2 - \varepsilon)$ so $E(X) = 10 / (1/2 + \varepsilon) \approx 20$. If we decide to roll $11$ times instead, we get the expected value $\approx 12$. So, no, it does not generalize. – MT_ Jul 23 '16 at 18:08
  • Well, even creating probability of $\frac{1}{19}$ does not generalize. we are better throw by groups of 3 , and not groups of 2. – d_e Jul 23 '16 at 19:02
  • @DavidC.Ullrich: I see you misunderstood my comment, but no problem. It is crucial to remember the discarded outcome. See my edited answer for the details and proof! =) – user21820 Jul 24 '16 at 09:10
  • @MichaelTong: See my edited answer for the general algorithm and proof; my earlier comment was too cryptic so you and David didn't get it. – user21820 Jul 24 '16 at 09:14
  • @AndréNicolas: In general one needs to remember the earlier rolls (I shouldn't have said "discarded"). I've explained in my edited answer. – user21820 Jul 24 '16 at 09:18
  • @d_e: Yes you're right that we cannot simply discard and re-roll the $6$-dice to get the optimal splitting into $19$ parts. But to see how the algorithm in my edited answer works in this case, we remember which of the $17$ branches we went down, so on the next roll it multiplies to $102 = 5 \times 19 + 7$ branches, so only in $7$ branches do we have to continue. Note that this is strictly superior to rolling in groups of 3. – user21820 Jul 24 '16 at 09:22
  • What is the initial value of y? – lebesgue Sep 27 '19 at 14:30
  • Also, how is x used? I only see it got assigned but not used anywhere. – lebesgue Sep 27 '19 at 15:23
  • @lebesgue: Thank you very much for spotting a typo! $x$ and $y$ were supposed to be the same. – user21820 Sep 28 '19 at 11:59
  • Also, i don't think the algorithm is quite right. Take k=6 and m=7 as an example, when we enter into the part inside the if statement, n = 36 and x = 0 at that time. Then, after roll the dice once, p is definitely going to be smaller than 6 and we will return floor(x/a) directly because x = p < am, which is not correct. – lebesgue Sep 30 '19 at 21:33
  • @lebesgue: Sorry about that. Is it correct now? – user21820 Sep 30 '19 at 21:50
  • Thanks, looks better now. But I think this line inside the loop should be removed - ''Set x:=x+p.   [Move down the chosen branch.]'' – lebesgue Oct 01 '19 at 13:19
  • @lebesgue: Thanks for checking! I have been real busy these few days so I was trying to make it quick with this. – user21820 Oct 02 '19 at 15:30