#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
using namespace std;
///////////////////////// 蒙特卡罗算法 Monte Carlo Algorithm MCA 解决 pi 问题 by hsluoyz ////////////////////////////////////
struct Sample{
//sample part
double x;
double y;
//sample part
};
int sum;
int got;
Sample smp;
double ObjectiveFunction(Sample asmp){
//object part
if (asmp.x * asmp.x + asmp.y * asmp.y <= 1)
return 1;
else
return 0;
//object part
}
int RandomIntLessThan(int aint){
return (int) ((double) aint * (double) rand() / (double) (RAND_MAX + 1));
}
double RandomDoubleLessThan(double adouble){
return adouble * (double) rand() / (double) (RAND_MAX);
}
int RandomTrueBy(double aprob){
double res = (double) rand() / (RAND_MAX + 1);
if (res <= aprob)
return 1;
else
return 0;
}
Sample GenSmp(){
//sample part
Sample res;
res.x = RandomDoubleLessThan(2) - 1;
res.y = RandomDoubleLessThan(2) - 1;
//cout << res.x << " " << res.y << endl;
return res;
//sample part
}
void main(){
srand((unsigned) time(NULL));
sum = 10000000;
got = 0;
for (int i = 0; i < sum; i ++){
smp = GenSmp();
if (ObjectiveFunction(smp))
got ++;
}
//object part
cout << "The pi is " << 4 * (double) got / (double) sum << endl;
//object part
}
MCA.rar_MCA_模拟 π
版权申诉
147 浏览量
2022-09-23
17:45:55
上传
评论
收藏 11KB RAR 举报
我虽横行却不霸道
- 粉丝: 72
- 资源: 1万+
最新资源
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
- 田间种植行排号自动生成工具
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈