jeudi 19 septembre 2019

How to generate an equally likely digit between 1 and 7 using one regular die?

import numpy as np
N = 400000
dice = np.random.randint(1, high=7, size=(N, 2))

from collections import Counter

repeat_list = [(die1, die2) for [die1, die2] in dice]

dice_counted = Counter(repeat_list)

excluded = dice_counted[(6, 6)]

total = sum(dice_counted.values()) - excluded

final_count = dict([(i, 0) for i in range(1, 8)])
for key, value in dice_counted.items():
    num1, num2 = key
    if num1 == num2:
        if num1 != 6:
            final_count[7] += value
    else :
        final_count[num1] += value

print('probability of each digit 1-7 using a regular die:')
for key, value in final_count.items():
    print('{}: {:.4f}%'.format(key, 100 * value/float(total)))

If the die is (6, 6), then ignore, which leaves 1/35 chance for all the rest combinations. It's easy to get 7 classes each with 5/35 probability out of the 35 combinations.




Aucun commentaire:

Enregistrer un commentaire