randnorm.cpp

来自「此文件包含了在linux下实现tpr-tree索引的源代码」· C++ 代码 · 共 46 行

CPP
46
字号
//--------------------------------------------------------------------//   randnorm.cpp//   ------------    //   Generation of normally distributed random numbers//   Adopted from Tom Schneider ////   Moving point workload generator v 1.1//   Copyright(c) 1999-2001, Aalborg University//#include "random.h"#include <math.h>/*   Method: if U is a member of the set [0..1] and Un and Un+1   are two members, then define      theta = Un 2 pi      r     = sqrt(-2 ln(Un+1))   then when these polar coordinates are converted to Cartesian   coordinates, one gets two independent Normally distributed numbers,   with mean 0 and standard deviation 1.  To get other standard deviations   multiply by a constant, and to get other means, add a constant.*/double randnorm (double mean, double deviation){   double uniform;   double theta, r;   double x;   do   {      theta = 2.0 * M_PI * rnd();      uniform = rnd();      // avoid bombing if the uniform happens to be zero      if (uniform != 0.0)	r = sqrt(-2 * log(uniform));   }   while (uniform == 0.0);   x = r * cos(theta);         // from polar to cartesian coordinates   return mean + x*deviation;  // return x coordinate}

⌨️ 快捷键说明

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