Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from typing import Tuple, Any, List
from itertools import starmap, chain, repeat
from random import choice
from collections import Counter

def sample_space(*values: Tuple[Any, float]) -> List:
    if sum(value[1] for value in values) != 1.0:
        raise ValueError('Soma das probabilidades não é 1.0')
    while True:
        if all(probability.is_integer() for _, probability in values):
            break
        values = [(value, probability * 10) for value, probability in values]
    values = [(value, int(probability)) for value, probability in values]
    return list(chain(*starmap(lambda a, b: list(repeat(a, b)), values)))

space = sample_space((1, 0.4), (7, 0.3), (15, 0.3))
samples = [choice(space) for _ in range(100)]

print(Counter(samples))