首先就是s = np.random.uniform(-1,1,(n,2)),这个是random函数里面的uniform distribution,就是均匀的随机分布,它的参数(-1,1这两个是产生数字的上下限,后边(n,2)是告诉它要产生一个n行2列的数组,这个数组里每个数字都是介于(-1,1)之间的。
import numpy as np
import matplotlib.pyplot as plt
# n: the number of points to be used in the simulation
def mc_pi(n):
s = np.random.uniform(-1,1,(n,2))
x=s[:,0]
y = s[:,1]
z = np.sum(np.sqrt(x**2+y**2)<1)*4/n
return z