It should be close to 0.3
$ cat monte.py
import random,math
density=int(1e6)
x = [random.uniform(0,1)*7*math.pi for _ in range(density)]
y = [random.uniform(0,1) for _ in range(density)]
i = [math.sin(xx)*math.cos(xx) > yy for (xx,yy) in zip(x,y)]
print sum(i)/(float(density)*10.0)*7*math.pi
$ python monte.py
0.350184850795
I am trying to rewrite the below but for some reason the python code is not even close.
x = rand(1, 1000000)*7pi;
y = rand(1, 1000000);
i = sin(x).* cos(x) >y;
Area3 = (sum(i) / 10000000)*7pi;
I’m getting identical results between your matlab and python versions… Are you sure that the matlab version is giving you ~2, and not ~0.35?
For example:
MATLAB:
This yields:
0.3511Your pure-python version:
This yields:
0.347935156296Numpy-based:
This yields:
0.350475133957