randn.cpp

来自「Visual C++写的」· C++ 代码 · 共 25 行

CPP
25
字号
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "randn.h"

double randn(const double Mean, const double SquareMargin)
{
	const double PI = 3.1415926;
	double model = 4294967296.0;
	double multiplicator = 663608941.0;
	double temp_a, temp_b;
	static double seed = (double)time(NULL);
	
	seed *= multiplicator;
	seed -= (int)(seed/model) * model;
	temp_a = seed/model;

	seed *= multiplicator;
	seed -= (int)(seed/model) * model;
	temp_b = seed/model;

	double norm_randn = sqrt(-2*log(temp_a))*cos(2*PI*temp_b);
	double gauss_rand = Mean + norm_randn*SquareMargin;
	return(gauss_rand);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?