In [3]:
from random import random as rand
In [4]:
def calculate_pi(n):
tot = 0
for i in range(n):
x = rand()
y = rand()
if x**2 + y**2 < 1.0:
tot += 1
return 4.0 * (tot / n)
In [5]:
%timeit calculate_pi(1000000)
In [6]:
import numpy as np
In [7]:
def calculate_pi(n):
x = np.random.rand(n)
y = np.random.rand(n)
tot = (x**2 + y**2 < 1.0).sum()
return 4.0 * tot / n
In [8]:
%timeit calculate_pi(1000000)
In [ ]: