C++随机数求圆周率

来源:互联网 发布:黑客查看网站php源码 编辑:程序博客网 时间:2024/05/20 05:57
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <iomanip>


using namespace std;


int main()
{
long long i = 0, d = 1000000000, c = 0;
float x = 0, y = 0;


srand((unsigned int)time(NULL));
for (long i = 0; i < d; ++i)          //这么大的数需要计算的时间还是挺久的,不过PI的值很接近了
{
x = (float)rand() / RAND_MAX;
y = (float)rand() / RAND_MAX;
if (sqrt(x * x + y * y) <= 1)
++c;
}
cout << setiosflags(ios::fixed) << setprecision(20) << acos(-1.0) << endl;


system("pause");
return 0;
}
原创粉丝点击