NANA
rand.cpp
1#include "..\include\math\rand.h"
2#include <cmath>
3
4namespace NANA {
5namespace MATH {
6
7
8
9
10
11
12
13double NA_API randNormal(double r_me, double sd)
14{
15 int i;
16 const int normal_count = 360;//样本数目采用360个
17 double ccl_num, ccl_s;
18 double ccl_ar[normal_count];
19 ccl_num = 0;
20 for (i = 0; i < normal_count; i++) {
21 ccl_ar[i] = rand() % 1000 / (double)999;//生成一个[0,1]的均匀分布
22 ccl_num += ccl_ar[i];
23 }
24 ccl_num -= ((double)normal_count * 0.5);//减去0-1均匀分布的均值
25 ccl_s = 1.0 * (double)normal_count / 12.0;//0-1分布的方差为1/12
26 ccl_s = sqrt(ccl_s);
27 ccl_num /= ccl_s;//此时ccl_num接近标准正态分布的一个子集
28 ccl_num *= sd;
29 ccl_num += r_me;
30 return ccl_num;
31}
32
34{
35 double x1, x2;
36 x1 = std::rand() % RAND_MAX / (double)RAND_MAX;
37 x2 = std::rand() % RAND_MAX / (double)RAND_MAX;
38 double ccl_num = std::sqrt(-2 * std::log(x1)) * cos(2 * NA_PI * x2);
39 return ccl_num;
40}
41
42}
43}
44
#define NA_API
兼容windows系统与linux系统
Definition: core_global.h:24
#define NA_PI
圆周率
Definition: nadef.hpp:35
double NA_API randNormalWitghBoxMuller()
Definition: rand.cpp:33
double NA_API randNormal(double r_me, double sd)
独立同分布的中心极限定理生成正态分布的随机数
Definition: rand.cpp:13