Today, after playing some interesting board games, I was wondering about some probabilities with dice. As far as I did not want to calculate them with statistics methods (and it is always funnier to make a simulation), I have decided to give it a try in Python.

So, let’s imagine that you have one dice 🙂 The question is, out of * n *throwings, how many throwings would be the same as the previous (or next)? This same as the previous sum would be

**. Probably it is better to give an example, than to explain it 🙂**

*m*E.g., let’s say that we throw the dice 10 times. And we get* 3,3,5,2,2,4,1,1,3,1 *in this sequence. Now, lets count, how many numbers are the same as the previous number (or the next one). In our case we have 6 such numbers * 3,3,5,2,2,4,1,1,3,1. *Pretty good. Now, if you are statistician and you have got my idea, try to do some calculations, of what is the expected

*, if we throw the dice 10 million times.*

**m**With python and my PC I have to wait about 20 seconds for this simulation. Anyway, 20 seconds is quite ok. I received a result for * m*= 30.56%. Later, I have tried with 100 million throwings and the result was the same – 30.56%. Thus, this is the result of my inquiry is that the expected m = 0.3056 *

*Tested and verified 🙂*

**n.**

You may try with even bigger numbers, but I am quite sure that the result would not be different. 100 million tries is quite satisfactory, when we are talking about dice throwing.

Here comes the code for the dice simulation:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import random def print_list(myResults, myList): total_percentage = 0 for x in range(0, len(myResults)): if myResults[x]: print("{} -> {}".format(x + 1, myResults[x])) percentage = 100 * ((x + 1) * myResults[x]) / len(myList) total_percentage += percentage print("{} %".format(round(percentage, 2))) print("Grand total percentage {}!".format(round(total_percentage, 2))) def simple_print(myList): for x in range(0, len(myList)): print("{}".format(myList[x])) myList = [] myResults = [0] * 100 generatedRandom = -1 previous = 0 countMatch = 0 for x in range(0, 10): if x > 0: previous = generatedRandom generatedRandom = random.randint(1, 6) if generatedRandom == previous: countMatch += 1 if countMatch > 1: myResults[countMatch - 1] = myResults[countMatch - 1] - 1 myResults[countMatch] = myResults[countMatch] + 1 else: myResults[countMatch] = myResults[countMatch] + 1 else: countMatch = 0 myList.append(generatedRandom) simple_print(myList) print("Matches:") print_list(myResults, myList) |

If you want to test it, change line 29 from 10 to 10 millions and run it. Comment line 46 as well.

Enjoy it!