zaxecivobuny

vic's monte carlo

Oct 17th, 2020
674
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3.  
  4. def single_test(number_of_dice, number_of_sides):
  5.     more_ones = 0
  6.     for i in range(number_of_dice):
  7.         result = random.randint(1, number_of_sides)
  8.         # print(result)
  9.         if result == 1:
  10.             more_ones += 1
  11.         elif result == number_of_sides:
  12.             more_ones -= 1
  13.  
  14.     return more_ones > 0
  15.  
  16.  
  17. def main():
  18.     number_of_dice = 4
  19.     number_of_sides = 4
  20.  
  21.     print(single_test(number_of_dice, number_of_sides))
  22.  
  23.     count_successes = 0
  24.     trials = 1000000
  25.     for i in range(trials):
  26.         if single_test(number_of_dice, number_of_sides):
  27.             count_successes += 1
  28.  
  29.     print(count_successes, trials, count_successes * 1.0 / trials)
  30.  
  31.  
  32. if __name__ == '__main__':
  33.     main()
  34.  
RAW Paste Data