fork download
  1.  
  2. # your code goes here
  3. import random
  4. stulya = {
  5. "S pikami" : {"weight": 4},# 50%
  6. "S huyami": {"weight": 3}, # 37.5%
  7. "Norm": {"weight": 1} # 12.5%
  8. }
  9.  
  10. start = 0
  11. points = []
  12. for key, value in stulya.items():
  13. start += value["weight"]
  14. points.append({ "point": start, "key": key })
  15. a = sorted(points, key=lambda s: s["point"])
  16.  
  17. choises = { key: 0 for key in stulya.keys()}
  18.  
  19. for i in range(100000):
  20. choise = random.uniform(0, start)
  21. left = 0
  22. for p in points:
  23. if left < choise <= p["point"]:
  24. choises[p["key"]] += 1
  25. break
  26.  
  27.  
  28. print(choises)
  29. total = sum([c for c in choises.values()])
  30. print("Vsego vibiraly {} raz".format(total))
  31. for key, value in choises.items():
  32. print("Varant '{}' vipal {} raz ili v {}% sluchaev".format(key, value, value*100/total))
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
Success #stdin #stdout 0.28s 12320KB
stdin
Standard input is empty
stdout
{'Norm': 12570, 'S pikami': 50172, 'S huyami': 37258}
Vsego vibiraly 100000 raz
Varant 'Norm' vipal 12570 raz ili v 12.57% sluchaev
Varant 'S pikami' vipal 50172 raz ili v 50.172% sluchaev
Varant 'S huyami' vipal 37258 raz ili v 37.258% sluchaev