正态随机分布 C++实现

转自:http://www.cnblogs.com/yeahgis/archive/2012/07/13/2590485.html

高斯分布也称为正态分布(normal distribution)

常用的成熟的生成高斯分布随机数序列的方法由Marsaglia和Bray在1964年提出,C++版本如下:

#include <stdlib.h>
#include <math.h>

double gaussrand() { 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; }
原文地址:https://www.cnblogs.com/coder2012/p/2789735.html