2

Given a Dice of range 1...n, and an integer range of 1...X, where n is less than X.

What is the minimum number of dice rolls you need to fairly pick a random number from the integer range 1...X?

[For example, a dice of 6 sides and a range of 1 to 10. The minimum number of rolls for a fair random number is 5 rolls, (you would sum all the roll values then divide by 5).] <- bad example, kept here for posterity.

  • Summing the values is a very bad idea, because that means values 'in the middle' are much more likey to come up. I'm not sure how ecactly you want to handle rounding in your example, but coming up with sum 30 would require all 5 rolls to be a 6. Coming up with sum of say 17 is much more likely. – Ingix Oct 20 '18 at 11:56
  • It depends on the values. If, say, $n=2,X=4$ then you can do it with two rolls since each of the possible ordered pairs of rolls is equi-probable. But for other values of $X$ you can just approximate the random selection. – lulu Oct 20 '18 at 11:58
  • Note: I don't understand what you mean by "range", I assumed you meant "number of faces", but from your example you appear to have meant something else. – lulu Oct 20 '18 at 12:00
  • @lulu: I think the OP just used 'range' twice, once for the number of faces of the dice and once for the number of possible outcomes of the 'random variable'. – Ingix Oct 20 '18 at 12:03
  • @ingix Well, does that make sense? If I have $n$ different values I can "code" them however I like. I can use a fair coin instead of a two sided die, setting $H=1$, $T=2$, say. $n$ values are $n$ values. Anyway, the OP ought to clarify. – lulu Oct 20 '18 at 12:12
  • If the die has 6 sides and you want a random number from 1 to 10, uniformly distributed, there is no way to guarantee an answer in a finite number of rolls. What is "minimum" then? Minimum expected number of rolls? Minimum rolls until you have a non-zero probability of having generated a random number? Something else? – David K Oct 20 '18 at 19:45
  • Compare this question: https://math.stackexchange.com/questions/1868680/creating-unusual-probabilities-with-a-single-dice-using-the-minimal-number-of-e – David K Oct 20 '18 at 19:46

3 Answers3

1

Averaging the rolls does not give a uniform distribution, as can be seen in the example of two six-sided dice. Instead, the standard procedure to roll a number between 1 and $x$ with an $n$-sided die is:

  • Roll $k$ times, where $k$ is any natural number such that $n^k\ge x$. From each roll subtract 1 so that the numbers lie between 0 and $n-1$ inclusive. Define $D=\lfloor n^k/x\rfloor$.
  • Interpret the sequence of numbers thus obtained as a base-$n$ number $r$. Compute $\lfloor r/x\rfloor$. If this is at least $D$, restart the process, else $(r\bmod x)+1$ is uniformly distributed between 1 and $x$ inclusive.

It may be desirable to make $k$ one more than the minimum possible for it to reduce the chances of rejection.

Parcly Taxel
  • 105,904
  • You can't bound the number of rolls with this method. If you are lucky you need $k$ rolls, but if you aren't, you will need $2k$, $3k$, or $10^{70} k$... – ajotatxe Oct 20 '18 at 12:14
  • @ajotatxe This is the only fair method. Rejection is not of serious concern, since the probability of no result decreases rapidly with time. – Parcly Taxel Oct 20 '18 at 12:16
  • @ajotatxe In short: perfection is merely a stretch goal in Dungeons & Dragons. – Parcly Taxel Oct 20 '18 at 12:17
  • Well, I must say that your method is what I'd do in real life (assimung that role playing is 'real life' xD). My method (my answer) is, theorically, the safest. I think. – ajotatxe Oct 20 '18 at 12:18
0

If you roll a die with $n$ faces $k$ times, there are $n^k$ possible outcomes. Assuming that the die is fair, these outcomes are equiprobable.

We need some way to assign a number from $1$ to $X$ to each outcome. The number of outcomes assigned to each number from $1$ to $X$ must be the same for all these numbers, that is, $X/n^k$. So $n^k$ must divide $X$. The minimum value for $k$ is that $n^k$ divides $X$. This is not always possible, though.

It is possible only if every prime factor of $X$ divides $n$.

For example, if $n=6$ and $X=8$, since $6=2\cdot 3$ and $8=2^3$, you need to roll the die three times, because $6^3$ is the least power of $6$ that is a multiple of $8$. If $X$ is $10$ is not possible, because $5$ is a prime factor of $10$ and no power of $6$ is a multiple of $5$.

If you have dices with different number of faces, you can optimize the process a bit. With common dices (4,6,8,10,12 and 20), $X$ can't have a prime factor different from $2$, $3$ or $5$.

ajotatxe
  • 66,849
0

The real answer came already from ajotatxe in the comments: There is no possible way to do this if $X$ has a prime factor that $n$ does not contain. No matter how many times ($k$) you make throws, you cannot put the resulting, equally probable $n^k$ sequences into $X$ bins with equal number of sequences in each bin.

What I like to elaborate on are 2 more things:

  1. The misconception shown in the example, and
  2. a way to do this, even if it might in theory take an unbounded number of throws.

1) Throwing a 6-sided dice will on average give you a $3.5$. If you throw one a hundred times, you are much more likely to come up with a sum near $350$ thnt with a sum near $100$ or $600$, the minimal and maximal values. That's because for a sum of $100$ all $100$ throws must be $1$, an obviously very unlikely outcome, where for a sum of $350$, you can have lots of any number from $1$ to $6$.

2) What you can do in your example (n=6, X=10) is the following: Throw your dice twice, writing down the result of the first throw, and writing down an additional $0$ if you trow $1-3$ on the second throw or a $6$ when you throw $4-6$ on the second throw. It's easy to see now that if you add the two numbers (your first throw and what you wrote down as result of the second throw), that you get a number between $1$ and $12$, uniformly distributed in that range.

If you got a number between $1$ and $10$, take this as you random number. If you get an $11$ or $12$, start over from the beginning (2 throws). This might in theory take you an unbounded number of rethrows, but in practice the probability of that goes to 0 fast.

Ingix
  • 15,665