2014-08-22 Emacs and Dice

Somebody asked on Google+: What are the odds for rolling 2d10 and a d20 and getting higher on the 2d10?

on Google+

(let ((a 0) (b 0) (n 0))
  (insert "2d10 vs. 1d20")
  (newline)
  (newline)
  (insert "     ")
  (dotimes (roll-1 10)
    (dotimes (roll-2 10)
      (insert (format " %5s"
		      (concat (number-to-string (1+ roll-1)) "+"
			      (number-to-string (1+ roll-2)))))))
  (newline)
  (dotimes (roll-3 20)
    (insert (format " %4d" (1+ roll-3)))
    (dotimes (roll-1 10)
      (dotimes (roll-2 10)
	(insert "     "
		(cond ((> (+ 2 roll-1 roll-2) (1+ roll-3))
		       (incf a)
		       "A")
		      ((= (+ 2 roll-1 roll-2) (1+ roll-3))
		       (incf n)
		       "-")
		      (t
		       (incf b)
		       "B")))))
    (newline))
  (newline)
  (let ((total (+ a b n)))
    (insert (format "A: %d / %d = %4.3f\n" a total (/ a 1.0 total)))
    (insert (format "B: %d / %d = %4.3f\n" b total (/ b 1.0 total)))
    (insert (format "-: %d / %d = %4.3f\n" n total (/ n 1.0 total))))
  (newline)
  (newline))

2d10 vs. 1d20

        1+1   1+2   1+3   1+4   1+5   1+6   1+7   1+8   1+9  1+10   2+1   2+2   2+3   2+4   2+5   2+6   2+7   2+8   2+9  2+10   3+1   3+2   3+3   3+4   3+5   3+6   3+7   3+8   3+9  3+10   4+1   4+2   4+3   4+4   4+5   4+6   4+7   4+8   4+9  4+10   5+1   5+2   5+3   5+4   5+5   5+6   5+7   5+8   5+9  5+10   6+1   6+2   6+3   6+4   6+5   6+6   6+7   6+8   6+9  6+10   7+1   7+2   7+3   7+4   7+5   7+6   7+7   7+8   7+9  7+10   8+1   8+2   8+3   8+4   8+5   8+6   8+7   8+8   8+9  8+10   9+1   9+2   9+3   9+4   9+5   9+6   9+7   9+8   9+9  9+10  10+1  10+2  10+3  10+4  10+5  10+6  10+7  10+8  10+9 10+10
    1     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    2     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    3     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    4     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    5     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    6     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    7     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    8     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
    9     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
   10     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A     A
   11     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A     -     A     A     A     A     A     A     A     A     A
   12     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A     B     -     A     A     A     A     A     A     A     A
   13     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A     B     B     -     A     A     A     A     A     A     A
   14     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A     B     B     B     -     A     A     A     A     A     A
   15     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A     B     B     B     B     -     A     A     A     A     A
   16     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A     B     B     B     B     B     -     A     A     A     A
   17     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A     B     B     B     B     B     B     -     A     A     A
   18     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A     B     B     B     B     B     B     B     -     A     A
   19     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -     B     B     B     B     B     B     B     B     -     A
   20     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     B     -

A: 1000 / 2000 = 0.500
B: 900 / 2000 = 0.450
-: 100 / 2000 = 0.050

So, the odds of rolling higher on 2d10 is 50%.

(Does anybody have some actual math on how to do this?)

​#Emacs ​#RPG

Comments

(Please contact me if you want to remove your comment.)

Uhhhh... distractions!

I am not sure if there is any **easy** math answer, it will probably require a loop of some kind (e.g. sum). But if you want to find a chance of something, then just do this:

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my $total = 0;
my $higher = 0;

while (1) {
  my $dice1 = int(rand(20)) + 1;
  my $dice2 = int(rand(10)) + 1;
  my $dice2 += int(rand(10)) + 1;
  $higher++ if $dice2 > $dice1;
  $total++;
  say "Total=$total Chance=" . ($higher / $total) if $total % 25000 == 0;
}

(You will have to write something like this to check your math answer anyway. Perl random number generator is good enough for several digits of precision)

After a few seconds I get:

Total=62725000 Chance=0.499959601434835

Which is effectively 50% too.

And yes, as mentioned by others, there is 5% of a tie.

But what is the chance to win? (Assuming that we will throw a dice again if there is a tie) Well, mathematically it probably means that we have to find a limit of some function. But... uh... Whatever:

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my $total = 0;
my $higher = 0;

while (1) {
  my $dice1 = int(rand(20)) + 1;
  my $dice2 = int(rand(10)) + 1;
  $dice2 += int(rand(10)) + 1;
  next if $dice2 == $dice1;
  $higher++ if $dice2 >= $dice1;
  $total++;
  say "Total=$total Chance=" . ($higher / $total) if $total % 25000 == 0;
}

Total=48175000 Chance=0.526481598339388

Which is probably 52.65%

– AlexDaniel 2014-08-23 01:16 UTC

AlexDaniel

---

Careful, math ahead.

For the 2d10, the probability of rolling m (for an \(2 <= m <= 20\)) are as follows: Overall, there are 100 combinations, each with probability \(1/100\). But we can ignore 45 of these (it doesn’t matter if we roll \((i, j)\) or \((j, i)\)). Let’s say we only deal with pairs \((i, j)\) where \(i <= j\). Each pair \((i, i)\) has odds \(1/100\), while each of the others has probability \(1/50\). For \(m <= 11\) and odd there are exactly \((m-1)/2\) ways of rolling it (just try it for m=1, 3 and 5 and you’ll see), so each has prob. \((m-1)/2/50=(m-1)/100\). For \(m <= 11\) and even there are \(m/2\) ways of rolling it, but one is of form \((i, i)\), so the probability is \(1/100 + (m-2)/2/50=1/100 + (m-2)/100 = (m-1)/100\). Hey, that’s the same as for the odd case! That’s convenient.

For \(m > 11\) the numbers work the same way as \(22-m\). For example 18 works the same as 4, so we can ignore these for now.

Let’s say we roll \(n <= 11\) on our 1d20. Then the probability of rolling higher than that is 1 - prob of rolling lower or equal. So let’s calculate that, because it’s easier. The probability of rolling lower or equal is the sum of the probabilities of all the cases where we roll something lower or equal, so \(\sum_{i=1}^n (i-1)/100 = sum_{i=0}^{n-1}i / 100 = n(n-1)/2/100 = n(n-1)/200\). To check: The chance of rolling 1 or lower is 0, the chance of rolling exactly 2 is \(1/100\), the chance of rolling lower or equal 3 is equal to the chance of rolling \((1,1)\) or \((1,2)\), so \(1/100 + 1/50 = 3/100 = 6/200\), which is what our formula gives us. Makes sense. So the chance of rolling higher than n is \(1 - n(n-1)/200\).

Now, for \(n > 11\) on our d20, the chance for rolling higher than n is equal to the chance of rolling lower than or equal to \(21-n\) (again, try it in your head; it doesn’t matter if we try to get higher than 20 or lower equal to 1; it’s just the same). So for each \(n>11\) we get chance \(q(q-1)/200\), where \(q = 21-n\), so for each q between 1 and 9.

Now, to sum it all up: each of the rolls on the d20 is equally likely \((1/20)\). So probability of rolling \(<= 11\) on the d20 and of winning against it is equal to \(\sum_{i=1}^{11} (1 - i(i-1)/200)/20 = 0.44\). Likewise, the probability of rolling \(> 11\) on the d20 and of winning against it is \(\sum_{i=1}^9 i(i-1)/200 = 0.06\)

So, the probability of winning with 2d10 against one d20 is 0.5, as you said.

That being said, I liked your emacs program better.

As to the chance to win, where you reroll a tie: that’s \(1/2 + 0.05 * 1/2 + 0.05 * 1/2 + ...\), so \(\sum_{i=0}^\infty 0.05^i * 1/2 = 0.526316\) exactly. The added chance of winning as compared to the case where we count a tie as a loss is so tiny because the probability of getting a tie is so small.

P.S.: I might or might not be looking for an RPG group in Zurich (which is how I found your page). Would you mind contacting me if you’re looking for players?

– Christian von Essen 2014-09-13 00:41 UTC

---

I’m going to try and use some MathJax to prettify your math. That’ll give me time to work it through. 😄

MathJax

– Alex Schroeder 2014-09-13 08:20 UTC