Advertisement
makispaiktis

Possibilities when I have n possible values and averageError

Apr 22nd, 2019 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4. print("This is a game that will show you the power of possibility and randomness,")
  5. print("when we have many iterations of a phenomenon based on luck/randomness")
  6. print("In this problem, I have a quantity that can take 'n' possible values (possibility = 1/n).")
  7. print()
  8. n = int(input("First, enter the number n, whose possibility (1/n) you want to check: "))
  9. print("You chose number " + str(n) + ". We all know that if we have " + str(n) + " numbers, the possibility of appearance one of them is: 1/" +str(n) + " = " + str(100/n) + "%.")
  10. simulations = int(input("Now, enter how many simulations you want me to run (prefer to be large number like 10000 or more): "))
  11. counters = []                              # For example, counters[0] will count how many 1 (assoi) are there, counters[1] how many 2 (dyaria) are there
  12.  
  13. for i in range(n):
  14.     counters.append(0)                     # After this for-loop, I will have n elements in my list and each element will have the value 0 (0 appearances yet)
  15.  
  16. for i in range(1, simulations+1):
  17.     r = random.randint(1, n)                # Create a number, whose value is: 1 or 2 or 3 or ..... or n
  18.     counters[r-1] += 1          # If r = 4 for example, I want to increase the counter that symbolizes the appearances of number 4
  19.                                             # so I have to increase by 1 the element r-1 in list "counters"
  20.  
  21. print("In this session, we have the following results: ")
  22. print()
  23. print("                Appearances        Possibility        % Possibility                Error")
  24. errors = []                         # In this list, I will put all the errors %, so I can find the average error
  25.  
  26. for i in range(n):
  27.     error = abs(counters[i]/simulations*100 - 100/n)
  28.     print("  Value " + str(i+1) + ":          " + str(counters[i]) + "             " + str(counters[i]/simulations) + "             " + str(counters[i]/simulations*100) + "%             " + str(error) + "%")
  29.     errors.append(error)
  30.  
  31. # For average error
  32. sum = 0.0
  33. for i in range(len(errors)):
  34.     sum += errors[i]
  35. averageError = sum / len(errors)
  36. print()
  37. print("Average error: " + str(averageError) + "%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement