Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- print("This is a game that will show you the power of possibility and randomness,")
- print("when we have many iterations of a phenomenon based on luck/randomness")
- print("In this problem, I have a quantity that can take 'n' possible values (possibility = 1/n).")
- print()
- n = int(input("First, enter the number n, whose possibility (1/n) you want to check: "))
- 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) + "%.")
- simulations = int(input("Now, enter how many simulations you want me to run (prefer to be large number like 10000 or more): "))
- counters = [] # For example, counters[0] will count how many 1 (assoi) are there, counters[1] how many 2 (dyaria) are there
- for i in range(n):
- 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)
- for i in range(1, simulations+1):
- r = random.randint(1, n) # Create a number, whose value is: 1 or 2 or 3 or ..... or n
- counters[r-1] += 1 # If r = 4 for example, I want to increase the counter that symbolizes the appearances of number 4
- # so I have to increase by 1 the element r-1 in list "counters"
- print("In this session, we have the following results: ")
- print()
- print(" Appearances Possibility % Possibility Error")
- errors = [] # In this list, I will put all the errors %, so I can find the average error
- for i in range(n):
- error = abs(counters[i]/simulations*100 - 100/n)
- print(" Value " + str(i+1) + ": " + str(counters[i]) + " " + str(counters[i]/simulations) + " " + str(counters[i]/simulations*100) + "% " + str(error) + "%")
- errors.append(error)
- # For average error
- sum = 0.0
- for i in range(len(errors)):
- sum += errors[i]
- averageError = sum / len(errors)
- print()
- print("Average error: " + str(averageError) + "%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement