Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/da70ba74dc8542a3268d to your computer and use it in GitHub Desktop.
Save lettergram/da70ba74dc8542a3268d to your computer and use it in GitHub Desktop.
import numpy as np
def func1(x, y):
return x*x + x*y + y*y
def func2(y, x):
return x**3 + y**3 + y*x**2 + y**2 + 2
def monte(func, n, ymin, ymax, xmin, xmax):
sum = 0
x = np.random.uniform(xmin, xmax, n)
y = np.random.uniform(ymin, ymax, n)
for i in range(n):
sum += func(x[i], y[i])
print(i, sum / (i+1))
area = abs(xmax - xmin) * abs(ymax - ymin)
return area * (sum / n)
total1 = monte(func1, 10000, 0, 1, 2, 4)
total2 = monte(func2, 10000, 1, 3, 1, 2)
print(total1, total2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment