一、问题描述:
假设圆的半径为1,那么圆的面积就是pi,正方形的面积是4,随机产生正方形中的点,那么这个点落在园的概率是园的面积 / 正方形的面积,g = pi / 4 ,这个点落在园内的判断:x*x + y*y <= 1
二、代码展示
def estimatePI (times ):
hits = 0
for i in range(times):
x=random ()*2-1
y=random ()*2-1
if x*x +y *y <=1:
hits +=1
return 4.0*hits/times
print(estimatePI (1000000))
三、输出结果:
3.135676