26

The exact question is: You need an 8-sided die for a game. You only have a coin, two four-sided and one 10-sided dice. How can you replace the 8-sided die? Re-rolls are not allowed.

There are several solutions to this I've been told, I found one, but my solution wasn't one of the expected solutions. What solutions can you think of?

My solution was:

Roll 10-sided and 2x 4-sided dice, sum up the result and roll and substract 2-sided die (the coin) which you divide by 2 at the end to get the result (round up)

  • 2
    There are lots of possible answers. If yours wasn't on the short list made up by the person asking the problem, that doesn't mean your solution is wrong. Also, the singular of "dice" is "die"; if the person asking the question made this error, that should give you some pause as well. – vadim123 Nov 14 '16 at 18:21
  • I get the impression that my way of thinking was so different then the interviewer expected, I assumed I get the question totally wrong. Also both he and I were not native english speakers so I didn't even think about that grammar error :) – Volkan Ulukut Nov 14 '16 at 18:22
  • Why don't you share YOUR solution, and we call tell you if you're right. – vadim123 Nov 14 '16 at 18:26
  • 1
    Closely related: How to generate a random number between 1 and 10 with a six-sided die?. Most of the solutions there can be adapted to this situation with a minimum of difficulty. – MJD Nov 14 '16 at 18:28
  • 10
    Your solution doesn't replace an 8-sided die, because it produces some values (such as 4) much more frequently than others (such as 1). – MJD Nov 14 '16 at 18:30
  • 23
    Remember that you need to not only generate random numbers between 1 and 8, but they also need to be uniformly distributed. – Riccardo Orlando Nov 14 '16 at 18:30
  • 50
    Isn't flipping the coin 3 times sufficient? – kennytm Nov 14 '16 at 18:32
  • 1
    I'm not sure you can use your dice/coin more than once. why would the question have 2x 4 sided dice then? – Volkan Ulukut Nov 14 '16 at 18:38
  • 6
    For example, I just had the computer try your procedure 10,000 times, and it produced the following results: $$\begin{array}{rr} 1 & 165\ 2 & 763\ 3 & 1510\ 4 & 1962\ 5 & 2089\ 6 & 1887\ 7 & 1187\ 8 & 400\ \end{array}$$ – MJD Nov 14 '16 at 18:38
  • 42
    Rolling a 4 sided die, and then adding 4 if a coin toss is heads, accurately simulates an 8 sided die. – Riccardo Orlando Nov 14 '16 at 18:39
  • so mine was indeed wrong. any right solutions you can think of? – Volkan Ulukut Nov 14 '16 at 18:39
  • 1
    @VolkanUlukut I suppose you are not required to use all of the dice. – Riccardo Orlando Nov 14 '16 at 18:42
  • Yes, I'm not, i got confused there I guess, but using one die multiple times is forbidden too. your answer works fine though. – Volkan Ulukut Nov 14 '16 at 18:44
  • @VolkanUlukut Also, your procedure sometimes produces the number 9. (If you roll $10 + 4 + 4 - 1$ you get 17; divide by 2 and round up and you have 9. – MJD Nov 14 '16 at 18:58
  • 4
    @vadim123 'Also, the singular of "dice" is "die"; if the person asking the question made this error, that should give you some pause as well.' Oh, please. It's perfectly possible for somebody who doesn't know that "dice" is a plural to compute probabilities. – David Richerby Nov 14 '16 at 20:00
  • 2
    @vadim123: According to Wikipedia both "die" and "dice" are possible singular forms. – celtschk Nov 14 '16 at 20:03
  • 29
    Not sure if this is allowed, but I would just roll the 10-sided die, and re-roll if it landed on a value above 8. – BradC Nov 14 '16 at 20:41
  • 2
    @celtschk, see here. Although some people do use "dice" as singular, it is still uncommon and not considered correct. Eventually the language may change, of course. – vadim123 Nov 14 '16 at 20:46
  • 3
    @vadim123: Why should I assume that the site you linked to is authoritative? The Oxford dictionaries certainly don't say the singular use is uncommon or incorrect. Quite the opposite (emphasis by me): "Historically, dice is the plural of die, but in modern standard English dice is both the singular and the plural: throw the dice could mean a reference to either one or more than one dice" – celtschk Nov 14 '16 at 20:57
  • 1
    @celtschk, wikipedia cites this same "Oxford Living Dictionary" you did, which is just one dictionary. However Cambridge, American Heritage, GNU, dictionary.com all give dice as plural. Merriam-Webster Learner's Dictionary agrees with you. So perhaps the language is in flux, but I disagree about standard English today. – vadim123 Nov 15 '16 at 01:12
  • @vadim123: I see a pattern here: British dictionaries list "dice" as singular, American dictionaries don't. So maybe it's a British English vs. American English issue. In any case, it's clearly wrong to label a singular "dice" as wrong, in particular on an international web site. – celtschk Nov 15 '16 at 07:52
  • By my count there are $\frac{320!}{40!^8}\simeq 10^{281}$ ways of doing this, which is quite a lot. – Oscar Cunningham Nov 15 '16 at 18:05
  • The problem as stated can be solved in many ways, all producing uniform distribution. Roll d10, reroll on 9s and 10s. Flip coin 3 times, intepret as binary 000b - 111b, roll 2d4 and interpret one as binary, as 00(base4) through 13(base4), use the coin or d10/5 instead of one of the d4, relabel the d4's and d10 as you wish, but the most fun--trade them for a d8! – ChuckCottrill Nov 16 '16 at 01:49
  • Even more fun--write a {bash,ruby,python,perl} script to generate random number between 1..8, and toss the obsolete dice :-). Or download a dice rolling app to your phone. Or call a friend who has a d8. Or lookup a dice rolling website, eg http://www.wizards.com/dnd/dice/dice.htm, https://rolz.org/ – ChuckCottrill Nov 16 '16 at 01:52
  • 1
    Roll d4,d10, read as 00..39(base10) modulo 8, pocket the coin. – ChuckCottrill Nov 16 '16 at 01:58
  • 3
    Go to store. Spend coin on an 8 sided die. Devote your math skills to winning the game. :) – candied_orange Nov 16 '16 at 03:49
  • More generally, you need 3 bits of information and it needs to be uniformly distributed. 1d4 + isEven( 1d2 || 1d4 || 1d10 ) ? 4 : 0 || 1d2 + isEven( 1d4 ) ? 2 : 0 + isEven( 1d4 || 1d10 ) ? 4 : 0 – Thaylon Nov 17 '16 at 08:35
  • @BradC Was going to tell the same, but it would require potentially infinite number of tosses. The solution with three coin flips has a guaranteed upper limit on number of tosses, not just an expectation. – polkovnikov.ph Nov 17 '16 at 11:56

13 Answers13

65

We need to not only generate numbers between 1 and 8, but also to make sure they are uniformly distributed.

Your solution does not produce uniformly distributed results (at least according to MJD, in the comments).

However, this procedure does: you can roll a 4-sided die for a value between 1 and 4, and then toss a coin: if heads, add 4 to the result.

It is easy to see that each number from 1 to 8 is produced by exactly one outcome: for example, a result of 3 requires a roll of 3 and a toss of tails, while a result of 5 requires a roll of 1 and a toss of heads.

Sklivvz
  • 981
  • 5
    For bonus points, you can simulate the coin toss by rolling again and taking evens/odds. –  Nov 15 '16 at 10:53
  • 16
    @Hurkyl for extra bonus points, you can use your simulated 8-die to simulate a 4-die, and a coin toss, to then simulate a different 8-die! – Riccardo Orlando Nov 15 '16 at 12:34
  • 2
    Roll the ten sided die to get t, and the four sided die to get f. Calculate (t+10*f)/5. – richard1941 Nov 15 '16 at 18:04
  • An even more interesting problem is how to test a die to determine if it is "fair". (We all know that dice games are never fair and that we are guaranteed by "gambler's ruin" to be wiped out.) – richard1941 Nov 15 '16 at 18:06
  • @RiccardoOrlando is my maths right? Getting any number between 1 to 4 is 1/4 and getting head and tail is 1/2 so every number have 1/2*1/4=1/8 chance of being thrown. – noman pouigt Nov 16 '16 at 01:38
  • roll d10, reroll 9s and 10s – ChuckCottrill Nov 16 '16 at 01:41
  • @nomanpouigt Correct! But remember to also state that getting exactly the right roll and exactly the right toss is the only way to get the number you want. – Riccardo Orlando Nov 16 '16 at 05:28
47

There are essentially three basic ways to generate a number from $\{1\dots k\}$ for $k\ne n$ with an $n$-sided die that preserves the uniform probability of all results:

  • Truncation: if $k\lt n$, you can simply ignore (reroll) results greater than $k$
  • Division: if $n=mk$ for some integer $m$, you can designate $m$ different results as giving a result $i$ for $1\le i\le k$ (i.e. to simulate a $3$-sided die with a $6$-sided die, you can designate $\{1,2\}\rightarrow 1$, $\{3,4\}\rightarrow 2$ and $\{5,6\}\rightarrow 3$)
  • Exponentiation: if $k=n^m$ for some integer $m$ you can roll the die $m$ times, interpreting the results as the digits of an $m$-digit integer in base $n$ (and interpreting a result of $n$ as $0$, and a string of all $0$'s as $k$) Example: percentile dice

Any combination of these can be used, thus for instance you could simulate an $8$-sided die with a $6\text{-sided}$ die by exponentiation by 2 (simulating a $36$-sided die) followed by division by 4 (simulating a $9$-sided die) followed by truncation to $8$. Since you have multiple dice to start with, more solutions are possible, but you only ever need one die. For instance you could simulate an $n\text{-sided}$ die for any $n$ with just a coin using exponentiation (generating binary strings with head $\rightarrow 1$ and tail $\rightarrow 0$) and truncation (rerolling results greater than $n$), and going the other way, you can simulate a coin with an $n$-sided die for any $n\ge 2$ by truncation to an even number (if $n$ is odd), followed by division to $2$.

If you have multiple die sizes, exponentiation can be generalized to multiplication (as is used in the accepted answer): if $k=mn$, and you have dice of sizes $m$ and $n$, you can roll the $n$-sided die (interpreting a result of $n$ as $0$) and add $n$ times the result of rolling the $m$-sided die (interpreting $m$ as $0$), and interpret $0$ as $k$. In the accepted answer $n=4$, $m=2$ and $k=8$, but an alternate solution would use $n=2$ and $m=4$ so you could (for instance) roll the $4$-sided die (interpreting $4$ as 0), multiply the result by $2$ then add $1$ if you flip heads, and interpret an overall result of $0$ (that is $[4,\text{tails}]$) as $8$. It's equivalent (and simpler) to multiply the $\text{d}4$ result by $2$ and subtract $1$ if you flip tails.

  • 1
    Good generalization yet, the question implied no rerolls. so first and third options are not applicable. and you can't do second since there aren't any 16-sided or more die you can roll. – Volkan Ulukut Nov 14 '16 at 18:47
  • 1
    Fair enough, I missed that part. I actually came up with these rules based on an old Google interview question about simulating a $8$-sided die using a $5$-sided die. I was quite happy that my solution (exponentiation to $25$, truncation to $24$, division to $8$) required fewer rolls on average than the supposed "correct" answer (truncation to $4$, division to $2$, exponentiation to $8$), even if you generated the three "bits" by $4 \times 2$ rather than $2 \times 2 \times 2$. – Gabriel Burns Nov 14 '16 at 18:53
  • Anything requiring a reroll could potentially (however unlikely) result in infinite rerolls (or a very long finite series) if the reroll number is continually repeated. – GRW Nov 14 '16 at 20:26
  • @GRW If by "anything requiring a reroll" you mean truncation (as opposed to exponentiation, which requires a fixed number of rolls), then yes, of course. What is your point? (no disrespect intended, I just honestly don't know what you're driving at here). – Gabriel Burns Nov 14 '16 at 20:28
  • 1
    @GRW If you're trying to say that truncation should be avoided because it requires a potentially infinite number of rolls, that's all well and good except that in the general case of simulating a $k$-sided die with an $n$-sided die, truncation is necessary if $k$ has any prime factors which are not also factors of $n$. – Gabriel Burns Nov 14 '16 at 20:35
  • If you're going to truncate anyway, you may as well just use the 10 sided die... lol – jpmc26 Nov 15 '16 at 20:31
  • @jpmc26 In the given case, yes, though as Volkan noted, rerolls aren't allowed, so truncation doesn't work in this case. As such, this answer is less of an answer to the actual question than a general treatment of the problem of simulating a $k$-sided die with an $n$-sided die. – Gabriel Burns Nov 15 '16 at 20:34
  • While I appreciate your convoluted example of rolling d8 with only one d6, there are much simplier veresion of doing this=) – TZakrevskiy Nov 15 '16 at 21:43
  • @TZakrevskiy I suppose that depends how you define simple. I think the way I gave uses the fewest possible rolls on average ($2.25$) using the methods I described here, though it doesn't use the fewest applications of the rules (for instance you could divide by $3$, then exponentiate by $3$, but this will always take $3$ rolls). By expanding the methods given here to include "splitting" with division, it is possible to use a trickier scheme that averages just $\frac {19} 9 $ rolls, but that would seem more "convoluted" to me. What method did you have in mind? – Gabriel Burns Nov 15 '16 at 22:05
  • The trickier scheme I mentioned goes as follows: exponentiation by $2$ (to $36$) if result $\lt32$ divide by $4$ (to $8$) otherwise, subtract $32$ (simulating a $\text{d}4$) and roll again, this time dividing by $3$ (to $2$), and use multiplication with the $\text{d}4$ result to simulate a $\text{d}8$. – Gabriel Burns Nov 15 '16 at 22:18
  • @GabrielBurns I meant the length of the thought and calculation process. The exponentiation by 2 and then division by four can be easily replaced by simply reducing d6 to d3 (often seen in tabletop games). In your terms, you first divide by two and then exponentiate by 2. And the simplest one would be divide by three, exponentiate by three, this is the most straightforward way in terms of algorithm length. – TZakrevskiy Nov 15 '16 at 23:21
  • I'm missing the part where it is implied that rerolls are not allowed; where are you getting this implication? – Tim Sparkles Nov 16 '16 at 01:03
  • Well, for one thing, the problem says you have two 4-sided dice. If rerolls are allowed, that's the same as having one 4-sided die. Also, as many people have mentioned, it's pretty trivial to simulate a $\text{d}8$ with a $\text{d}10$ if rerolls are allowed (just reroll $9$'s and $10$'s) – Gabriel Burns Nov 16 '16 at 14:34
32

Roll the D10 and if you roll 9 or 10, re-roll it. It's easy to remember and easy to do. And it gives uniform results.

Dave
  • 421
14

I'm assuming that all dice (counting the coin as 2-sided die) are rolled in parallel, and rerolls are not allowed. I'll use the standard notation D$n$ for an $n$-sided die (D2 for the coin).

Since we have to simulate a D8, which is a power of 2, we need to multiply uniform distributions with powers of 2; we can consider them as random bits.

  • The D2 delivers one random bit.

  • Each D4 delivers two random bits.

  • The D10 delivers only one random bit, as 2 is the highest power of 2 that divides 10. Since rerolls are not allowed, the factor 5 is useless for generating uniform distributions for powers of 2.

So we have 6 bits in total, of which we can select arbitrary 3 to generate a single D8 roll.

For example, you can use one D4 (2 bits) and the D2 (1 bit) to get 3 bits (this is the solution other answers gave).

You can also take both D4s, and use only one bit for one of them, for example by adding 4 to the result of the second D4 if the first D4 gives an odd result.

Or you could select arbitrary 3 dice, and take their bit value as 0 if the roll result is even and 1 if the result is odd, and then from the three bits form $4a+2b+c+1$ where $a$, $b$ and $c$ are the bits derived from the three dice.

Indeed, you could use the four given dice to simulate rolling two D8 in parallel!

celtschk
  • 43,384
3

Let the following denote:

  • $a$: the value of the $10$-sided die , i.e., $a\in[1,10]$
  • $b$: the value of the 1st $4$-sided die, i.e., $b\in[1,4]$
  • $c$: the value of the 2nd $4$-sided die, i.e., $c\in[1,4]$
  • $d$: the value of the $2$-sided coin , i.e., $d\in[1,2]$

Then the value of the $8$-sided die as a function of the above variables is:

$$f(a,b,c,d)=[32(a-1)+8(b-1)+2(c-1)+(d-1)]\bmod8+1$$


Here is a short Python script which confirms uniform distribution:

dict = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0}

for a in range(1,10+1):
    for b in range(1,4+1):
        for c in range(1,4+1):
            for d in range(1,2+1):
                dict[(32*(a-1)+8*(b-1)+2*(c-1)+(d-1))%8+1] += 1

print dict

The output is {1: 40, 2: 40, 3: 40, 4: 40, 5: 40, 6: 40, 7: 40, 8: 40}.

barak manos
  • 43,109
  • 1
    But a and b have no effect in your equation, since 32 mod 8 = 0 and 8 mod 8 = 0. – Justin Nov 14 '16 at 20:05
  • 2
    Of course $32(a-1)\equiv 0 \pmod{8}$ and $8(b-1)\equiv 0 \pmod{8}$, so your first two terms amount to nothing but an elaborate way to write $0$. Moreover for all possible values of $c$ and $d$, we have $2(c-1)+(d-1)<8$, therefore after removing the first two terms, the $\mod 8$ also becomes superfluous, so the formula simplifies to $2(c-1)+d$. – celtschk Nov 14 '16 at 20:12
  • @Justin: You're right, the $8$-sided die can be simulated using $c$ and $d$ only (i.e., one of the $4$-sided die and the coin). – barak manos Nov 14 '16 at 21:45
  • @celtschk: You're right, the $8$-sided die can be simulated using $c$ and $d$ only (i.e., one of the $4$-sided die and the coin). – barak manos Nov 14 '16 at 21:45
3

Roll 2 four-sided dice. Take the result of the first die. Check the second die; if odd, keep the result of the first die. If even, add four the result of the first die.

This method is essentially the same as rolling a d4 plus a coin flip, but can be done more easily as both dice can be rolled at once.

  • 1
    It would be less confusing to roll a 4-sided die and the 10-sided die. (Use the 10-sided die's oddness or evenness to decide whether to add 4.) This would prevent getting confused about which 4-sided die is being used for each purpose. – Jasper Nov 17 '16 at 04:30
  • @Jasper As someone who regularly rolls many dice at once, it's not confusing at all. Use two different colored dice, or roll the same d4 twice in a row. – LegendaryDude Nov 17 '16 at 13:19
2

I would only use the coin, flipping it three times. Each time you flip tails you mark down a 0 and when you flip heads you mark down a 1. Because you are flipping 3 coins this method can give you $2^3 = 8$ different binary numbers (e.g. 000, 001, 010, 011 etc...) in the range from 0 to 7. Just add 1 to the result and you are good to go!

This is valid because every result is equally likely to happen, with probability $\frac{1}{2}^3 = \frac{1}{8}$ just like it is on a 8 sided die.

Enuff
  • 49
  • 1
    The question says re-rolls are not allowed. Flipping the coin multiple times counts as a re-roll. – LegendaryDude Nov 17 '16 at 20:12
  • When I first read the question in my mind I somehow applied this restriction just for dice since you don't "roll" a coin. But you are right and this makes the question more interesting of course. – Enuff Nov 21 '16 at 07:52
2

There are many solutions. It is a 3 bits problem, since the numbers from 1 to 8 can be represented by 3 bits. Lets use a 4-side dice to decide on 2 bits, and any uniform binary solution for the 3rd one: the coin is the most evident way (as in the accepted answer), but not the only one.

MJim
  • 31
0

An ugly way to do it:

Roll all three dice. (4 * 4 * 10 = 160 possibilities)

If the d10 is in the range of [1, 8], use the d10 value. (4 * 4 * 8 = 128 ways)
Else: . (32 ways left:)
If the total is 11, use 1. . (1 way to get 1)
If the total is 12, use the d10 value minus 7. . (2 ways to get 2; 1 way to get 3)
If the total is 13, use 12 minus the d10 value. . (3 ways to get 3; 2 ways to get 2)
If the total is 14 and the d10 value is 9, use 4. (4 ways to get 4)
If the total is 14 and the d10 value is 10, use 1. (3 ways)
If the total is 15 and the d10 value is 9, use 8. (3 ways)
If the total is 15 and the d10 value is 10, use 5. (4 ways to get 5)
If the total is 16, use 16 minus the d10 value. . (3 ways to get 6; 2 ways to get 7)
If the total is 17, use the d10 value minus 3. . (2 ways to get 7; 1 way to get 6)
If the total is 18, use 8. . (1 way to get 8)

Jasper
  • 393
0

Assuming a D10 is numbered [1-10] and a D4 [0-3]: Roll the D10. keep the value for [1-8] For 9 and 10, roll a D4. Result is 1+D4 for 9 and 5+D4 for 10.

You have to roll 1 1/5 times in average for each D8 emulation.

castor
  • 1
0
  • D2 results $a$ in $\{0,4\}$
  • first D4 results $b$ in $\{1,2,3,4\}$
  • second D4 and D10 are just for fun

Roll all dices and coin and get $x=a+b$

0

To expand on MJim's excellent answer, you can use any combination of dice for which the product of their faces is a multiple of $8$. You then just need to find a rule to assign a number from $1$ to $8$ to each of these possibilities.

Now, I'd like to open up the problem a bit to other dice combinations for practical use, particularly in a tabletop RPG setting.

  • Theoretically, you can do $1d24\ mod\ 8 + 1$, but 24-sided dice are uncommon.
  • Limiting yourself to the dice from the question but removing the coin and duplicates, you can do $n = ((1d4 - 1) * 10 + 1d10)$ to generate $n$ from a uniform distribution over $[\![1;40]\!]$ and then do $n\ mod\ 8 + 1$.
  • Using only platonic solids, you can replace the d10 by a d20, resulting in a distribution over $[\![1;80]\!]$.
  • Using only "tumbling" dice, i.e. dice with right or obtuse angles between their adjacent faces, i.e. not d4, you can replace the $1d4 - 1$ with $1d12\ mod\ 4$.
  • Using only the d20: $((1d20\ mod\ 4) * 20 + 1d20)\ mod\ 8 + 1$.
-1

To keep the uniform repartition, here are some proposals :

  • Roll the 10-face dice, re-roll on 9 and 10.
  • Roll the 4-face dice, then flip the coin. Add 4 on heads.
  • Roll the 4-face dice and multiply the result by 2. Flip a coin, and substract 1 on heads.

There are probably a number of other solutions, the important part is to keep the repartition uniform.