fork download
  1. import random
  2.  
  3. def get_random_int():
  4. list = [
  5. (1, 0.2),
  6. (2, 0.6),
  7. (3, 0.1),
  8. (4, 0.1),
  9. ]
  10. acc = 0
  11. rand = random.random()
  12. for tuple in list[:-1]:
  13. acc += tuple[1]
  14. if rand < acc:
  15. return tuple[0]
  16. return list[-1][0]
  17.  
  18. for _ in range(1, 10):
  19. print(get_random_int())
Success #stdin #stdout 0.04s 11788KB
stdin
Standard input is empty
stdout
2
2
1
2
1
2
4
4
1