fork(2) download
  1. from math import sqrt
  2. import random
  3.  
  4. bonus = 1
  5. iterations = 10000000
  6.  
  7. def d(x): return random.randint(1,x)
  8.  
  9. results = [0]*(10-bonus)
  10. for i in range(iterations):
  11. target = 10
  12. tries = 1
  13. success = False
  14. while not success:
  15. success = (d(10)+bonus>=target)
  16. if success:
  17. break
  18. else:
  19. tries+=1
  20. target-=d(3)
  21. results[tries-1]+=1
  22.  
  23. average = sum([results[i]*(i+1) for i in range(len(results))])/iterations
  24. deviation = sqrt(sum([results[i]*(i-average)**2 for i in range(len(results))])/(iterations-1))
  25.  
  26. print("Average: {:.3f}+-{:.3f}".format(average,deviation))
  27. print("Number of Tries | "+" | ".join([str(x).rjust(6) for x in range(1,1+len(results))]))
  28. print("Chance | "+" | ".join([str(round(x/iterations*100,3)).rjust(6) for x in results]))
  29.  
Success #stdin #stdout 1.72s 12344KB
stdin
Standard input is empty
stdout
Average: 2.535+-1.133
Number of Tries |        1 |        2 |        3 |        4 |        5 |        6 |        7 |        8
Chance          |   20.064 |   31.744 |   28.488 |   14.809 |    4.214 |    0.639 |    0.041 |    0.001