Question

(10 pts This problem deals with continuous (rather than discrete probability, but its an interesting problem! It involves probabilistically estimating the value of t. Consider a circle of radius 1 inscribed within a square with side 2. Both shapes are centered at the origin (0,0) Using basic geometry, the ratio of the circles area to the squares area is nu(1)2/22 TV4 Now, suppose that you randomly throw some darts at this figure. Out of n total (0,0) attempts, m attempts land within the circle. As the number of attempts becomes large, the ratio mn should approach the ratio of the circles area to the squares area. Thus, we can write 1t/4 mln. Solving for t gives us t 4m/n Write a Python program that allows the user to enter a value for n (the total number of attempts). Your program should then simulate throwing n darts at the figure by randomly picking coordinates Cx, y) between -1.0 and 1.0 Keep track of the darts that land within the circle, and show the resulting estimate for T. Python hint: Pythons random0 function (located in the random module) behaves the same as Javas Math random0-it returns a uniformly distributed pseudorandom real number in the interval 10.0, 1.00. To call it import random x random random

0 0
Add a comment Improve this question Transcribed image text
Answer #1

from random import uniform
from math import sqrt

def getRandomPoint():
return uniform(-1, 1)

def isInUnitCircle(x, y):
if sqrt(x*x + y*y) <= 1:
return True
else:
return False

n = int(input("Enter n: "))
count = 0
for i in range(0, n):
x = getRandomPoint()
y = getRandomPoint()
if isInUnitCircle(x, y):
count += 1

pie = (4.0*count)/n

print("Calculated value of pi = %f" % (pie))


# pastebin link: https://pastebin.com/X9YQsTJi

Add a comment
Know the answer?
Add Answer to:
This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT