Python – Times the dice is same as previous or next

 

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.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 m. Probably it is better to give an example, than to explain it 🙂

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 m, if we throw the dice 10 million times.

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 * n. Tested and verified 🙂

diceSimulation

 

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:

 

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

Enjoy it!

 

 

About

VBA Developer

Tagged with: , , , ,