gasdev.c

来自「IBM(原)数据生成器和源代码」· C语言 代码 · 共 28 行

C
28
字号
#include <math.h>#include "dist.h"float NormalDist::gasdev(void)// returns a normally distributed deviate with zero mean and unit// variance, using ran1(idum) as the source of uniform deviates. {  float fac,rsq,v1,v2;  if  (iset == 0) {		// We don't have an extra deviate handy, so    do {      v1=2.0*(*ran1)()-1.0;	// pick two uniform numbers in the square      v2=2.0*(*ran1)()-1.0;	//   extending from -1 to +1 in each direction,      rsq=v1*v1+v2*v2;		// see if they are in the unit circle    } while (rsq >= 1.0 || rsq == 0.0);	// and if they are not, try again.    fac=sqrt(-2.0*log(rsq)/rsq);    // Now make the box-Muller transformation to get two normal    // devites. Return one and save the other for next time.    gset=v1*fac;    iset=1;		// Set flag.    return v2*fac;  }   else {		// We have an extra deviate handy,    iset=0;		// so unset the flag,    return gset;	// and return it.  }}

⌨️ 快捷键说明

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