Assignment 4: Needles in your Pi
Due by: Friday, September 29, 2023 at 11:59 p.m.
In 1777, Georges-Louis Leclerc, Comte de Buffon, posed a problem in geometric probability:
If there is a floor made of strips of wood in which all the strips have the same width and a needle is dropped on the floor, what is the probability that the needle will land such that it crosses one of the edges between strips?
Specification
In this assignment we will make the simplifying assumption that the needle is equal in length to the width of the strips. It turns out that the answer to Buffon's question in this case is 2/π.

In a file called assignment4.py, write a function with the following header that that uses Buffon's needle to approximate π by Monte Carlo simulation, similar to the example from the book. Use Polya's problem solving strategy to think through how you will do this.
def buffonApproximation(needles):
Within this function you will iterate over the number of needles specified and randomly select a location for each needle. Perhaps the most challenging part will be determining whether each needle crosses a line.
Your approximation for π will be two times the total number of needles divided by the number of needles that cross a line. The function should return this value rather than print it out.
Extra Credit
You can earn up to 20% extra credit for this assignment by creating a visualization of your buffonApproximation() function using turtle graphics, similar to the one above. The visualization does not have to look exactly like the example, but it should show each needle and make it clear which ones cross a line.
Turn In
Upload assignment4.py
to Blackboard.
All work must be submitted before Friday, September 29, 2023 at 11:59 p.m. unless you are going to use a grace day.
All work must be done individually. You may discuss general concepts with your classmates, but it is never acceptable for you to look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.
Grading
Your grade will be determined by the following weights:
Category | Weight |
---|---|
Generating needle locations | 40% |
Testing to see if needles cross lines | 40% |
Returning a valid approximation to π | 20% |
Making a meaningful visualization of needle locations | 20% |
Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.