Skip to content

Instantly share code, notes, and snippets.

@pat-hanbury
Last active August 1, 2022 09:59
Show Gist options
  • Save pat-hanbury/fe93e4fed08736175bd292529cc76efc to your computer and use it in GitHub Desktop.
Save pat-hanbury/fe93e4fed08736175bd292529cc76efc to your computer and use it in GitHub Desktop.
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