I'm trying to solve this problem for school. I wrote some code for it, however I am not sure if my code is correct because when I checked website (linked below) for the formula to solve the problem, my output does not align with the answers of the formula, I was wondering what I am missing or if I am doing something wrong.
This is the link with the problem and the mathematical solution: https://math.stackexchange.com/questions/1110006/n-players-throw-a-die-and-get-score-for-each-pair-who-throw-the-same-number
I only have the mean so far because I didn't bother to get the variance seeing that the results of the mean were different.
This is the code:
import random
dice = [1, 2, 3, 4, 5, 6]
def dice_a (n, sims):
scores= []
sim_counter = 0
scores_ctr = {}
for x in range(n // 2):
scores_ctr[x + 1] = 0
while sim_counter != sims:
score = 0
rolls = random.choices(dice, k=n)
pairs = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
for x in rolls:
pairs[x] += 1
if pairs[x] == 2:
score += 1
pairs[x] = 0
scores.append(score)
scores_ctr[score] += 1
sim_counter += 1
mean = 0
for x, y in scores_ctr.items():
mean += x * (y/1000)
return mean
print(dice_a(7, 1000))
Hope to get some help!
Thank you so much!