Difference between revisions of "Monte Carlo Calculation of Pi"

From WLCS
(Created page with "'''Objective:''' * To become well-learned in the way of the while loop '''Resources:''' * [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteratio...")
 
Line 16: Line 16:
 
# Calculate an estimate of pi
 
# Calculate an estimate of pi
 
#* successProbability = numHits / N
 
#* successProbability = numHits / N
*# pi = successProbability / 4
+
#* pi = successProbability / 4
 
# Print out your estimate of pi
 
# Print out your estimate of pi

Revision as of 13:56, 26 September 2013

Objective:

  • To become well-learned in the way of the while loop

Resources:

Directions:

  1. Prompt the user for a number N (this will be our total number of test points
  2. Create a variable for our numHits
  3. Write a loop that runs N times
    1. Generate random numbers for x and y
    2. Use the distance formula to calculate the distance from (0, 0) to (x, y)
    3. Increment (Increase by 1) numHits if the distance is less than 1
  4. Calculate an estimate of pi
    • successProbability = numHits / N
    • pi = successProbability / 4
  5. Print out your estimate of pi