Question
In the code cell below, write Python code to accomplish the following using what you've learned through Chapter 4. Let's continue our exploration of Pi
In the code cell below, write Python code to accomplish the following using what you've learned through Chapter 4. Let's continue our exploration of Pi by creating a new estimate using Monte Carlo simulation. The approach will follow Example 1 from the following article:
Estimating using Monte Carlo simulations
This technique relies on simulation and random numbers to estimate the value of Pi essentially by throwing darts at a board inscribed with a circle and counting how many darts land inside the circle and how many land outside the circle. For everyone's safety, let's stick to a computer-based simulation! To simplify, we'll use the first quadrant unit square as suggested in the article.
The approach will work as follows:
1. For each dart thrown, generate its coordinates, x and y, using a random uniform distribution from 0 - 1. The Python function random.random will include the 0 endpoint but exclude the 1 endpoint. The Python function random.uniform may include the 1 endpoint. See Python documenation for random module for more details.
2.If the dart is inside the quarter-circle, count it. A dart is inside the quarter-circle if its distance from the origin (0, 0) is <= 1. You may use the simplified distance formula: =sqrt(2+2
3. After all the darts have been thrown and counted, Pi may be estimated as: =4/
Write a function named estimate_pi_mc that takes a single integer parameter, the number of darts to throw, and returns the estimate of Pi using this Monte Carlo simulation approach. Also include code that will prompt the user for the number of darts they wish to use in the estimate. Call your function with this value and display the estimate with 6 digits of precision. Use sentinel-control to repeat this process until the user enters a number of darts that is <= 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started