5

we have a six-sided dice numbered from 1 to 6, and each has the probability $p =\frac{1}{6}$. My task is to create a unbiased method to generate a random number from 1 to 10 using the given dice. I am aware that the method should maps the following set $$ \{1,\dots 6\}^k\mapsto \{1,\dots, 10\}$$, with $k$ is the number of time we throw the dice. For sure not the 36 elements will be mapped into the set containing 1 to 10. What i mean, the method will use a while loop and only stops if a certain tuple is found. My task then is the find those tuples and prove each thoese 10 tuples has the probaitly of $\frac{1}{10}.$ I think $k$ should be 2 in this case , but then i am not quite sure how to extract the 10 elements from 36 elements ($6^k$,with $k=2$) and have a probability of $p'=\frac{1}{10}$. can we maybe also generalize that for$ n$ elements ?

$$ \{1,\dots 6\}^k\mapsto \{1,\dots, n\} $$ I will appreciate any good ideas.

Mohbenay
  • 319
  • 2
  • 10

5 Answers5

4

Here is the algorithm for $n=10$.

  1. Throw the dice twice to get $(x,y)$.
  2. Compute $r=x + 6(y-1)$. If $r\le10$, return $r$ and stop.
  3. If we have reached here, go back to step 1.

Here is the algorithm for general $n$.

  1. Compute $n=\lceil \log_6k\rceil$.
  2. Throw the dice $n$ times to get $(x_1, x_2, \cdots, x_n)$.
  3. Compute $r=x_1+6(x_2-1)+6^2(x_3-1)+\cdots+6^{n-1}(x_n-1)$. If $r\le k$, return $r$ and stop.
  4. If we have reached here, go back to step 2.

There are many variations, of course.

For example, in step 2 for $n=10$, we can return $r=10 - (x + 6(y-1))\%10$ and stop when $x+6(y-1)\le30$ instead. This will reduce the chance to go to step 2. Here % is the remainder operator.

For example, here is gnasher729's algorithm for $n=10$. Throw a dice until we get a number $x\not=6$. Throw the dice once more to get another number $y$. Return ((x - 1) * 6 + (y - 1))%10+1$.


(Exercise 1.) (One minute or less) Devise an algorithm for $n=3$.

(Exercise 2.) Devise an algorithm for $n=7$. Try making its expected number of rolls as small as possible.

(Exercise 3.) Prove that the expected number of rolls in gnasher729's algorithm is the minimum among all algorithms.

(Exercise 4.) Given any $n>1$, devise an algorithm with the least expected number of rolls.

John L.
  • 39,205
  • 4
  • 34
  • 93
2

Throw a dice until your number is not a six, giving a number x = one to five. Throw the dice once more, giving a number y = one to six. There are thirty combinations.

Calculate (x - 1) * 6 + (y - 1), take the last digit of the result, then add 1.

gnasher729
  • 32,238
  • 36
  • 56
1

If you want unbiased result, then:

  1. Throw first dice, this is uniform in {1-6}
  2. Throw another one, check parity if it is odd add 6
    • Here it doesn't matter what split you choose, just aim at $\frac{1}{2}$ probability.
  3. if result is 11 or 12 discard results and start from 1.

$k$ is 2 here, to get one result, but since this is rejection sampling, at average you need more than one pair. We cannot reuse failed sample, as this introduces bias.

By the way, it is tempting to multiply two results, discard anything that is greater than 27 and divide by 3 discarding reminder, but it is cutted $2\sigma$ Gaussian distribution, not uniform.

Solution for 1-10 is hand tailored, but with general procedure you could generalise to 1 to N uniform random distribution with M-sided dice or use bits of entropy extraction to produce bit sequences.

Evil
  • 9,525
  • 11
  • 32
  • 53
1

This is not an answer but a global analysis.

I see 3 answers here which are exactly the same. In any you may have to re-roll infinitly dices. The question is why ?

Just build the prime factorization of:

  • you want 10 = 5 x 2
  • you have 6 = 3 x 2

A dice cannot create a random (with homogeneous probability) choice which is not one of its prime component. Thus build the 2 is easy but the 5 is basically impossible. You have to cancel (re-roll) some values to change the nature of your dice. Here, one side of a dice is re-rolled to build a 5-sided dice.

You can apply this analysis to any values for this problem.

Optidad
  • 1,788
  • 1
  • 10
  • 12
0

Here's a general solution: Assume you have an unbiased dice with D faces, numbered 0 to D-1 (for example D = 6). Assume you want to find one of N numbers, 0 to N-1.

Start with s = 0, S = 1. s is the current state, S is the number of possible states. We will always have 0 ≤ s < S.

Throw the dice, throwing a number d, 0 ≤ d ≤ D-1. Replace s with sD + d, and S with SD. Calculate m = S % N. If s < S - m then finish with the number s % N (the number of possible values s < S - m is a multiple of N). Otherwise, replace s with s - (S - m), replace S with S - (S - m), and throw the dice again.

gnasher729
  • 32,238
  • 36
  • 56