So Einstein was wrong when he said, “God does not play dice.” Consideration of black holes suggests, not only that God does play dice, but that he sometimes confuses us by throwing them where they can’t be seen.

Stephen Hawking

** **

I would recommend reading this post about the Law of Large Numbers before continuing with this one:

I want to run a quick and easy experiment which is in line with the Law of Large Numbers. That is to roll a dice for several times and see whether the average converges to the true value. One would expect that with more trials, the average of all dice rolls should converge to the true value, which is 3.5. Let’s see if that is actually the case.

Here is how it goes: A dice will be rolled several times, and the cumulative average is going to be calculated for every toss.

[pastacode lang=”python” manual=”%23%20Econowmics.com%0A%0A%23Importing%20the%20modules%20that%20I%20want%0A%23This%20modules%20helps%20me%20produce%20random%20numbers%0Aimport%20random%0A%23I%20will%20use%20this%20module%20to%20plot%20the%20graphs%20I%20want%0Aimport%20matplotlib.pyplot%20as%20plt%20%0A%23I%20need%20the%20numpy%20module%20because%20I%20was%20lazy%20to%20write%20a%20function%20to%20calculate%20the%20cumulative%20sum%20%3AD%0Aimport%20numpy%20as%20np%0A%0A%23Setting%20the%20seed%0Arandom.seed(1991)%0A%0A%23Defining%20a%20function%20for%20rolling%20the%20dice%0Adef%20Roll(n)%3A%0A%20%20%20%20%22%22%22This%20function%20simulates%20rolling%20a%20dice%20for%20n-times%22%22%22%0A%20%20%20%20%0A%20%20%20%20%23Empty%20vector%20to%20store%20the%20result%20of%20dice%20roll%0A%20%20%20%20result%20%3D%20%5B%5D%0A%20%20%20%20%0A%20%20%20%20for%20i%20in%20range(1%2Cn%2B1)%3A%0A%20%20%20%20%20%20%20%20result.append(random.choice(%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D))%0A%20%20%20%20return%20result%0A%0A%0A%23Now%2C%20lets%20roll%20the%20dice%20for%20a%20certain%20amount%20of%20times%0Aresult%20%3D%20Roll(1000000)%0A%0A%23Now%2C%20I%20want%20to%20plot%20how%20the%20average%20of%20dice%20rolls%20converge%20to%20the%20expected%20value%20(3.5)%0A%23Empty%20list%20to%20store%20the%20values%20of%20averages%0Aaverages%20%3D%20%5B%5D%0A%0A_cumsum%20%3D%20np.cumsum(result)%0Afor%20index%20in%20range(len(_cumsum))%3A%0A%20%20%20%20averages.append(_cumsum%5Bindex%5D%2F(index%2B1))%0A%0A%23Plotting%20the%20averages%0Aplt.plot(averages)%0A%0A%23Adding%20information%20to%20the%20graph%0Aplt.xlabel(‘Trials’)%20%23naming%20the%20x-axis%0Aplt.ylabel(‘Average%20of%20all%20outcomes’)%20%23naming%20the%20y-axis%20%0Aplt.title(‘Average%20value%20of%20Dice%20rolled%20’)%20%23Graph%20title” message=”” highlight=”” provider=”manual”/]

This code is very simple. First, I have created a function which simulates rolling a dice: The function randomly chooses a number from all possible outcomes of a coin toss: [1, 2, 3, 4, 5, 6]). The function will then add the results to a pre-defined vector, which I will later use to calculate the averages from. Then the cumulative sum of the results is calculated with Numpy’s cumsum function, so that the averages can be calculated. A graph shows the final results.

## #10 Trials

After just 10 trials, the average is already converging very quickly, standing at 3.8. First dice roll was a 5.

## #100 Trials

With 100 rolls, the average is already very close to true value.

## #1000 Trials

After 100 trials the average falls to about 3.25 and then again converges to 3.5.

## #1000000 Trials

With one million dice rolls, the average is very very close to the true value, being 3.4998015132783884