Dice Throwing Simulation with Numpy

Random Better Than Rigged

We can simulate dice throwing with Numpy and Python on this short code snippet

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

### Simulate 6 side dices
dice = np.array([i for i in range(1, 7)])
dice

### Random select 1 element of the array
### np.random.choice could be a loaded dice
choice = np.random.choice(dice)
choice

### Simulate Rolling the dice  10000 times 
n = 10000
rolls = np.array([np.random.choice(dice) for d in range(n)])
rolls

### Compute stats of rolls
m = np.mean(rolls)
v = np.var(rolls)

print(f"Mean is {m} and Variance is {v}")

### Histogram of Dice throw
n_rolls_hist = sns.histplot(rolls, discrete=True)
n_rolls_hist.set(title=f"Histogram of {n} rolls")
plt.show()

Dice throwing is used as an analogy in quantum mechanics to explain probabilistic concepts, particularly the idea that a particle's state is not determined until measured, much like the outcome of a die roll is unknown until it stops.

Semiconductor engineers also often have to simulate the charge state of nanometer chip component like a dice throwing simulation. They only can know probabilistically what the charge could be for these tiny components just like we only can know when throwing a 6 side dices, we will observe or measure the result as number 6 for 16.67% of time.

Want to Receive Updates On Fastest AI Models, Successful AI Startups and New Hiring Candidates. Subscribe To My Newsletters
Subscribe