Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- # 1. Intro
- print("******************************************")
- print("******************************************")
- print("In this programme, I suppose that I have 60 circles. Each circle")
- print("can have one of the following radius: r = 0.5 or 1 or 1.5 or 2 or or 2.5 or 3")
- print("1. There are 25 circles with radius = 0.5.")
- print("2. There are 20 circles with radius = 1.")
- print("3. There are 7 circles with radius = 1.5.")
- print("4. There are 5 circles with radius = 2.")
- print("5. There are 2 circles with radius = 2.5.")
- print("6. There is 1 circle with radius = 3.")
- print()
- print("We know that the average radius is: ")
- correctAverageRadius = (25*0.5 + 20*1 + 7*1.5 + 5*2 + 2*2.5 + 1*3) / 60
- print("(25*0.5 + 20*1 + 7*1.5 + 5*2 + 2*2.5 + 1*3) / 60 = " + str(correctAverageRadius))
- print("I will try to prove that this value of average radius can be found if I make this experiment: ")
- print("I will make many simulations of choosing one random circle, so its radius will be random too.")
- # 2. Create a list with each radius (25 first elements equal to 0.5, 20 following elements equal to 1, ....)
- firstRadius = 0.5
- secondRadius = 1
- thirdRadius = 1.5
- fourthRadius = 2
- fifthRadius = 2.5
- sixthRadius = 3
- list = []
- for i in range(25):
- list.append(firstRadius)
- for i in range(20):
- list.append(secondRadius)
- for i in range(7):
- list.append(thirdRadius)
- for i in range(5):
- list.append(fourthRadius)
- for i in range(2):
- list.append(fifthRadius)
- list.append(sixthRadius)
- # 2. Ask the user how many choices/simulations to run
- simulations = int(input("Enter how many simulations would you like me to run (prefer to be a big number such as 10000): "))
- sum = 0
- for i in range(simulations):
- randomIndex = random.randint(1, 60) # Create a number from 1 to 60 (1 <= r <= 60)
- sum += list[randomIndex-1] # The randomIndex which is generated is the index of the list
- # and the index of the list is related to the different values of radius
- averageRadius = sum / simulations
- print()
- print("Average radius: " + str(averageRadius))
- error = abs(averageRadius - correctAverageRadius)
- print("Error: " + str(100 * error) + "%.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement