8

I want to simulate a fair die roll N times without actually rolling a die. To be clear, I want to achieve something like this:

N = 1000
"1" showed up 185 times
"2" showed up 162 times
"3" showed up 158 times
"4" showed up 148 times
"5" showed up 182 times
"6" showed up 165 times

I can say that, for example, the number 1 shows up

random_binomial_distribution(N = 1000, probability = (1/6))

times. But I can't apply this for all faces, as the sum of all these results will likely not equal 1000.


How can I simulate the frequency of each face for N rolls without iterating through every roll?

Peter Mortensen
  • 271
  • 3
  • 8
Stack Danny
  • 183
  • 6
  • 1
    You already accepted one of the answer, so what is the point of your edit? Also, normal distribution is not correct in here, since it is not a distribution for counts. – Tim Sep 20 '21 at 10:36
  • I wanted to change `normal_distribution` to `random_normal_distribution` as it was kind of wrong. So which distribution do I have to use here? `binomial_distribution`? – Stack Danny Sep 20 '21 at 10:39
  • As described in the answer you accepted, you need to use the algorithm that uses pseudo-random generator for binomial distribution. – Tim Sep 20 '21 at 10:42
  • I understand now, binomial distribution is relevant when talking about integers, as otherwise the truncation of a normal distribution produces slightly different numbers. Very important to understand :) – Stack Danny Sep 20 '21 at 10:50
  • 5
    Seems to duplicate this from a couple of weeks ago: https://stats.stackexchange.com/questions/543536/how-to-sample-n-observations-from-a-multinomial-distribution-using-binomial-o/ – Glen_b Sep 20 '21 at 15:58
  • I looked at the linked post and I agree, however as I was searching for this topic I would have never found the mentioned post as I, as a hobby coder, am lacking the correct terminology for this kind of topic. I accepted the post as duplicate. – Stack Danny Sep 21 '21 at 12:07

2 Answers2

18

The frequencies for the different faces aren't independent, since they have to sum to $N$. However, instead of iterating over the dice, you can iterate over the number of faces, which doesn't increase with $N$:

  1. Simulate occurrences of 1, $n_1$, as Binomial, with $N$ trials and success probability $1/6$.
  2. Simulate occurrences of 2, $n_2$, as Binomial, with $N - n_1$ trials and success probability $1/5$.
  3. Simulate occurrences of 3, $n_3$, as Binomial, with $N - n_1 - n_2$ trials and success probability $1/4$.

And so on, until you simulate the occurrences of 6 with a success probability of $1$, using up all the remaining rolls.

  • 8
    +1 the algorithm is described by Devroye (p 559) http://www.nrbook.com/devroye/Devroye_files/chapter_eleven.pdf – Tim Sep 20 '21 at 10:12
1

I feel the simplest approach for a die of $D$ sides (your question assumes $D=6$), with an arbitrary set of values (your question assumes a die roll is one of $\in {1,2,3,4,5,6}$) is to use a shuffle algorithm-based sampling function on the set of values of your die:

Python

Assume you have an array of die values (any number of values, any set of values), named MyDie, the basis for "rolling" MyDie N times is:

import random
values, counts = np.unique(random.choices(MyDie,k=10), return_counts=True)

For example, for $N=10$:

>>> import random
>>> import numpy as np
>>> values, counts = np.unique(random.choices(MyDie,k=10),return_counts=True)
>>> print(values,"\n",counts,sep="")
[1 2 3 4 5 6]
[1 4 1 1 1 2]

R

Assume you have an array of die values (any number of values, any set of values), named MyDie, the basis for "rolling" MyDie N times is:

sample(MyDie,size=N,replace=TRUE)

For example, for $N=10$:

> MyDie <- 1:6
> table(sample(MyDie,size=10,replace=TRUE))

1 2 3 5 6 
4 1 2 1 2
Alexis
  • 26,219
  • 5
  • 78
  • 131