In: Computer Science
Python program to simulate estimate the probability that a random chord on a unit-circle (radius one), exceeds the radius of the circle? Repeat the experiment of generating random chords 1,000 times. Record the estimate of the probability that the chord length exceeds the radius
Please use comments to help explain
Code:-
import random # to generate random angles using randrange
function
import math # to access the sin,cos and radians function.
def ranLengthChord():
""" here the function takes no input & returns
a chord of randome length inside a unit circle"""
#calculate the random angles.
A1Deg = random.randrange(0,361)
A1Rad = math.radians(A1Deg)
A2Deg = random.randrange(0,361)
A2Rad = math.radians(A2Deg)
#calculate the coordinates of the end points of the chord.
pointx1=math.cos(A1Rad)
pointy1=math.sin(A1Rad)
pointx2=math.cos(A2Rad)
pointy2=math.sin(A2Rad)
#calculate the distance between the points using distance
formula
d=((pointx1-pointx2)**2+(pointy1-pointy2)**2)
dist = math.sqrt(d)
# return the length of the chord
return dist
exceeded=0 # storing the exceeds where chord length is greater than
unity
"""loop to call the randLengthChord func. 10000 times
and checks if the length is greater than unity """
for i in range(10000):
dist1 = ranLengthChord()
if dist1>1:
exceeded+=1
#output
print("Probability that chord exceeds the
radius:",exceeded/10000)
Output:-
Screenshot of Code:-
if u like my answer then please give a thumbs up..!!