4 ms·
A little bit of knowledge about stats and the appropiate language helps make this exercise easier. The R language is good at this kind of simulation. It comes w
by ImaCake 5y ago
A little bit of knowledge about stats and the appropiate language helps make this exercise easier. The R language is good at this kind of simulation. It comes with a whole bunch of random number generators out of the box. Here is how to estimate frequency of 7 from sums of two d6 dice rolls in R in two lines:
n <- 1e06
table(round(runif(n,1,6))+round(runif(n,1,6)))[6]/n
To break this down:
`n <- 1e06` assigns 1 million to n.
We want two six sided dice so we sample a random uniform (continous) distribution and round to a whole number:
`round(runif(n,1,6))`
This will make a vector of 1 million integers representing random "rolls" of the dice. In R it is really easy to pair-wise add two vectors of the same length so to get two dice we do:
`round(runif(n,1,6))+round(runif(n,1,6))`
Now we have a million rolls of summed pairs of dice. We want the frequency count of 7s. We can get this by getting the frequency count of every dice with `table()`:
`table(round(runif(n,1,6))+round(runif(n,1,6)))`
Which will return a print out of frequencies for every number that appears in the vector.
We can select 7s by pulling the 6th index and then dividing by the `n` dice rolls to get the frequency:
`table(round(runif(n,1,6))+round(runif(n,1,6)))[6]/n`
We can do the exact same oneliner in python using numpy and the Counter() function from collections:
`Counter(np.around(np.random.uniform(1,6,size=10*6))+np.around(np.random.uniform(1,6,size=10*6)))[7]/10*6`
Will return the frequency of 7s in python3.