fork download
  1. from typing import Tuple, Any, List
  2. from itertools import starmap, chain, repeat
  3. from random import choice
  4. from collections import Counter
  5.  
  6. def sample_space(*values: Tuple[Any, float]) -> List:
  7. if sum(value[1] for value in values) != 1.0:
  8. raise ValueError('Soma das probabilidades não é 1.0')
  9. while True:
  10. if all(probability.is_integer() for _, probability in values):
  11. break
  12. values = [(value, probability * 10) for value, probability in values]
  13. values = [(value, int(probability)) for value, probability in values]
  14. return list(chain(*starmap(lambda a, b: list(repeat(a, b)), values)))
  15.  
  16. space = sample_space((1, 0.4), (7, 0.3), (15, 0.3))
  17. samples = [choice(space) for _ in range(100)]
  18.  
  19. print(Counter(samples))
Success #stdin #stdout 0.02s 37824KB
stdin
Standard input is empty
stdout
Counter({1: 35, 15: 33, 7: 32})