用投针法估算概率
来源:互联网 发布:蜂群算法 matlab 编辑:程序博客网 时间:2024/04/30 11:38
问题描述:
x,y满足期望为mu,方差为sigma^2的正态分布,估算(x,y)落在(x^2+y^2-1)^3-x^2y^2=0内的概率。要求精确到小数点后1位。
c++代码:
#include <iostream.h>#include <stdlib.h>#include <math.h>using namespace std;double gaussrand(){//生成期望为0,方差为1的高斯分布随机数 static double V1,V2,S; static int phase = 0; double X; if(phase == 0) { do { double U1 = (double) rand() / RAND_MAX; double U2 = (double) rand() / RAND_MAX; V1 = 2 * U1 - 1; V2 = 2 * U2 - 1; S = V1 * V1 + V2 * V2; } while (S >= 1 || S == 0); X = V1 * sqrt(-2 * log(S) / S); }else X = V2 * sqrt(-2 * log(S) / S); phase = 1-phase; return X;}int leartCurve(double mu1,double sigma1,double mu2,double sigma2){//生成x,y并验证是否满足条件 double x = gaussrand(); double y = gaussrand(); double res; int count = 0; x = x*sigma1*sigma1 + mu1; y = y*sigma2*sigma2 + mu2; double x_2 = x*x; double y_2 = y*y; if(pow(x_2+y_2-1,3)-x_2*y_2 >=0){ return 1; } else return 0;}int main() { double res; int count = 0; for(int i=1;i<100000;i++) count += leartCurve(0,1,0,1);//记录满足条件的(x,y)个数 res = (double)count/100000;//估算概率 printf("%.1f",res);//保留1位有效数字}
注:由于题目要求只保留1位有效数字,因此循环100000次即可满足条件。
0 0
- 用投针法估算概率
- 用概率来估算圆周率
- 用概率来估算圆周率
- 贝叶斯:先验概率估算后验概率
- 小样本时的概率估算
- 爱的针法
- 估算
- 估算
- 估算
- 估算
- 估算
- 等概率投骰子
- 概率
- 概率
- 概率
- 概率
- 概率
- 概率
- 昇润CC2640 SDK应用入门教程二
- Mybatis入门
- 前端(1)——HTML标记语言
- 使用cookie实现记录上一次的登录名和密码
- oracle for循环,要注意的一点
- 用投针法估算概率
- 嵌套查询
- ElasticSearch安装、插件
- github初体验,将本地项目上传github上
- /sbin/insserv:No such file or directory
- MyBatis中进行批量与单个别名的定义
- SpringMVC 集成velocity
- github正式启用啦
- c++ fhog学习资料整理