9
$\begingroup$

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?

$\endgroup$
6
  • 1
    $\begingroup$ 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. $\endgroup$
    – Tim
    Sep 20, 2021 at 10:36
  • $\begingroup$ 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? $\endgroup$ Sep 20, 2021 at 10:39
  • $\begingroup$ As described in the answer you accepted, you need to use the algorithm that uses pseudo-random generator for binomial distribution. $\endgroup$
    – Tim
    Sep 20, 2021 at 10:42
  • $\begingroup$ 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 :) $\endgroup$ Sep 20, 2021 at 10:50
  • 5
    $\begingroup$ Seems to duplicate this from a couple of weeks ago: stats.stackexchange.com/questions/543536/… $\endgroup$
    – Glen_b
    Sep 20, 2021 at 15:58

2 Answers 2

21
$\begingroup$

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.

$\endgroup$
1
2
$\begingroup$

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
$\endgroup$
4
  • 1
    $\begingroup$ This is close, you also need to tabulate the values. e.g. table() in R. $\endgroup$ Sep 20, 2021 at 19:03
  • $\begingroup$ +1 @AccidentalStatistician Ah thank you! How is my edit (to both Python and R)? $\endgroup$
    – Alexis
    Sep 20, 2021 at 21:10
  • 5
    $\begingroup$ What you're suggesting is an algorithm with an $O(n)$ time while the accepted answer gives an algorithm that does not depend on $n$. $\endgroup$
    – Tim
    Sep 20, 2021 at 21:57
  • 1
    $\begingroup$ @Alexis Can't say for Python, but the R one looks good now for giving the desired answer, speed issues aside. $\endgroup$ Sep 20, 2021 at 22:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.