-
-
Save pat-hanbury/fe93e4fed08736175bd292529cc76efc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crude_monte_carlo(num_samples=5000): | |
""" | |
This function performs the Crude Monte Carlo for our | |
specific function f(x) on the range x=0 to x=5. | |
Notice that this bound is sufficient because f(x) | |
approaches 0 at around PI. | |
Args: | |
- num_samples (float) : number of samples | |
Return: | |
- Crude Monte Carlo estimation (float) | |
""" | |
lower_bound = 0 | |
upper_bound = 5 | |
sum_of_samples = 0 | |
for i in range(num_samples): | |
x = get_rand_number(lower_bound, upper_bound) | |
sum_of_samples += f_of_x(x) | |
return (upper_bound - lower_bound) * float(sum_of_samples/num_samples) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment