Method from That Class Python Question
Description
- and use a for loop to create 5 random points. (Note: you’ll need to import the random module to get numbers between 0 and 1, which you can send as arguments for x and y in the Point constructor.)
- Use the method from that class to to calculate the distance of each point from the origin for all 5 points.
- Print out the 5 points.
Your output will look something like this:
Sample 0 is: ( 0.11471474769443413 , 0.3491816944590306 )
Distance from origin: 0.36754228203551964
Sample 1 is: ( 0.7961816110068073 , 0.2519538252406587 )
Distance from origin: 0.8350963344182487
Sample 2 is: ( 0.4792875820033966 , 0.48809981627822585 )
Distance from origin: 0.684074569702383
Sample 3 is: ( 0.5255371295631991 , 0.378753604810677 )
Distance from origin: 0.6477990179883025
Sample 4 is: ( 0.7965471984894288 , 0.2921313660556841 )
Distance from origin: 0.8484268822090195
The idea is to imagine a square inside of which is inscribed a circle with a radius of 1. Any point inside the square may or may not also be inside the circle because while the circle is completely contained within the square, the corners of the square are outside the circle. We know that the area of a circle is A = πr2, so the ratio of points that fall within the circle and not the square should be 3.14159.. to 4 (consider, the area of this square is 2×2 = 4). So give 4000 points, we would expect about 31415 of them to be within the circle, and the rest to be in the corners. If we took a million points, how many should be within the square? That’s the key idea that will allow you to code this solution.
Import the math module into your program. You can get a very close approximation of pi by printing math.pi — that’s data, not a method, so no parenthesis.
Now you will want to estimate pi yourself by creating a million random points, and seeing how many fall within the circle. How do you know if a random point is in the circle or outside of it? Recall that the circle we are modeling has a radius of 1. If the random point is more than 1 unit away from the origin in our model, it’s outside.
use one million points in your simulation and:
- Calculate and print out how many of the points in the simulation fall within the circle modeled by your program
- Print out the actual value of pi according to Python’s math module
- Calculate and print out the estimated value of pi according to your Monte Carlo simulation
- Calculate and print out the difference between the actual and simulated value pi
Your finished output may look something like this:
Sample 0 is: ( 0.35068785782177514 , 0.11324034323689292 )
Distance from origin: 0.36851777292287397
Sample 1 is: ( 0.5389232611881516 , 0.40900797588824733 )
Distance from origin: 0.6765543627749907
Sample 2 is: ( 0.38472628641220064 , 0.12478783764413526 )
Distance from origin: 0.404458057010145
Sample 3 is: ( 0.6433022507367676 , 0.7044265510623535 )
Distance from origin: 0.9539677938193687
Sample 4 is: ( 0.08902634180720548 , 0.280591375471971 )
Distance from origin: 0.2943759662826196
785855 of the 1000000 are inside.
actual pi is about 3.141592653589793
estimated pi is about 3.14342
monte carlo simulation is off by -0.001827346410206765
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."