Difference between revisions of "Monte Carlo Calculation of Pi"
From WLCS
(6 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
'''Resources:''' | '''Resources:''' | ||
+ | * [[Media:PythonWhileLoops.pptx]] | ||
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration] | * [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration] | ||
+ | * [http://bkm.billking.io/projects/pi/ Monte Carlo Pi simulator] | ||
+ | * [http://math.fullerton.edu/mathews/n2003/montecarlopimod.html Monte Carlo Pi] | ||
* [http://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] | * [http://en.wikipedia.org/wiki/Monte_Carlo_method Monte Carlo method] | ||
− | |||
'''Directions:''' | '''Directions:''' | ||
− | # Prompt the user for a number N (this will be our total number of test points | + | # import random (at the top b/c you will be generating random numbers) |
− | # Create a variable for our '''numHits''' | + | # Prompt the user for a number N (this will be our total number of test points) |
− | # Write a loop that runs N times | + | # Create a variable for our '''numHits''' (this is *not* our loop counter) |
− | ## Generate random numbers for '''x''' and '''y''' between 0 and 1.0 by using '''random.random()''' | + | # Write a loop that runs N times (you should use a loop counter variable like '''count''' and avoid using '''x''' because we will use '''x''' for something else) |
+ | ## Generate random numbers for '''x''' and '''y''' between 0 and 1.0 by using '''random.random()''' (Note: this function does not need any parameters) | ||
## Use the distance formula to calculate the distance from (0, 0) to (x, y) | ## Use the distance formula to calculate the distance from (0, 0) to (x, y) | ||
## Increment (Increase by 1) '''numHits''' if the distance is less than 1 | ## Increment (Increase by 1) '''numHits''' if the distance is less than 1 | ||
# Calculate an estimate of pi | # Calculate an estimate of pi | ||
#* successProbability = numHits / N | #* successProbability = numHits / N | ||
− | #* | + | #* PI = successProbability * 4 |
− | # Print out your estimate of | + | # Print out your estimate of PI |
Latest revision as of 13:09, 1 February 2019
Objective:
- To become well-learned in the way of the while loop
Resources:
- Media:PythonWhileLoops.pptx
- HTTLACS: Ch 6 - Iteration
- Monte Carlo Pi simulator
- Monte Carlo Pi
- Monte Carlo method
Directions:
- import random (at the top b/c you will be generating random numbers)
- Prompt the user for a number N (this will be our total number of test points)
- Create a variable for our numHits (this is *not* our loop counter)
- Write a loop that runs N times (you should use a loop counter variable like count and avoid using x because we will use x for something else)
- Generate random numbers for x and y between 0 and 1.0 by using random.random() (Note: this function does not need any parameters)
- Use the distance formula to calculate the distance from (0, 0) to (x, y)
- Increment (Increase by 1) numHits if the distance is less than 1
- Calculate an estimate of pi
- successProbability = numHits / N
- PI = successProbability * 4
- Print out your estimate of PI