产生高斯分布随机数
2015-11-09 19:44
204 查看
方法一:
使用由Box和Muller提供的,在Knuth的网上讨论过的方法:
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
double gaussrand()
{
static double U,V;
static int phase = 0;
double e;
if(phase == 0)
{
U = (rand()+1.0)/(RAND_MAX+2.0);
V = rand()/(RAND_MAX+1.0);
z = sqrt(-2 * log(U)) * sin(2* PI *V);
}else
{
z = sqrt(-2 * log(U)) * cos(2* PI *V);
}
phase = 1 - phase;
return z;
}
方法二:
使用最初由Marsaglia提供的方法:
#include <stdio.h>
#include <math.h>
double gaussrand()
{
static double V1,V2,S = 0;
static int phase = 0;
double X;
if(phase == 0)
{
do{
U1 = (rand()+0.0)/(RAND_MAX+0.0);
U2 = (rand()+0.0)/(RAND_MAX+0.0);
V1 = 2* U1 - 1;
V2 = 2* U2 - 1;
S = V1*V1 + V2*V2;
}while( S >= 1 || S == 0);
X = sqrt( -2 * log(S) / S ) * V1 ;
}else
{
X = sqrt( -2 * log(S) / S ) * V2
;
}
phase = 1 - phase;
return X;
}
使用由Box和Muller提供的,在Knuth的网上讨论过的方法:
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
double gaussrand()
{
static double U,V;
static int phase = 0;
double e;
if(phase == 0)
{
U = (rand()+1.0)/(RAND_MAX+2.0);
V = rand()/(RAND_MAX+1.0);
z = sqrt(-2 * log(U)) * sin(2* PI *V);
}else
{
z = sqrt(-2 * log(U)) * cos(2* PI *V);
}
phase = 1 - phase;
return z;
}
方法二:
使用最初由Marsaglia提供的方法:
#include <stdio.h>
#include <math.h>
double gaussrand()
{
static double V1,V2,S = 0;
static int phase = 0;
double X;
if(phase == 0)
{
do{
U1 = (rand()+0.0)/(RAND_MAX+0.0);
U2 = (rand()+0.0)/(RAND_MAX+0.0);
V1 = 2* U1 - 1;
V2 = 2* U2 - 1;
S = V1*V1 + V2*V2;
}while( S >= 1 || S == 0);
X = sqrt( -2 * log(S) / S ) * V1 ;
}else
{
X = sqrt( -2 * log(S) / S ) * V2
;
}
phase = 1 - phase;
return X;
}
相关文章推荐
- Redis安装与使用
- 大数据架构之:Storm
- 遥控器油门摇杆电位器封装尺寸图
- 使用errorPage无法跳转到设定的error.jsp页面
- HD_2092整数解
- invalid username/password logon denied
- 8数码问题的C++解决方案
- 文本挖掘之文本聚类(OPTICS)
- 【基于Jsoup】Android通过Jsoup抓取网页信息详解(一)
- 用 vagrant 实现虚拟开发环境的快速部署
- 编程之美-求数组的子数组之和的最大值方法整理
- Swing getSource() 使用的注意事项
- Trinea性能优化之布局优化
- 工厂方法模式
- 大道至简第六章读后感
- 【转】 关于data factory的介绍——即如何快速生成大批量数据
- CodeForces 321C Ciel the Commander
- 数据库连接池,线程池,C3P0
- C++调试时要让窗口一直打开的方法
- Hadoop Hive sql语法详解