标签:png random image color .com ble 计算 include range
from random import random from math import sqrt from time import clock DARTS=1000000 hits=0.0 clock() for i in range(1,DARTS+1): x,y=random(),random() dist=sqrt(x**2+y**2) if dist<=1.0: hits=hits+1 pi=4*(hits/DARTS) print("pi值是{}.".format(pi)) print("运行的时间是:{:.5f}s".format (clock()))
此为课本上的pi的解法
下为c的解法:
#include<stdio.h> void main() { double pi=0.0,i=1.0,j=1.0; for(;i<1000000;i+=2,j=-j) pi+=1/(i*j); pi=pi*4; printf("pi=%lf",pi); }
标签:png random image color .com ble 计算 include range
原文地址:https://www.cnblogs.com/luyingqian/p/10544797.html