?? random.cpp
字號:
#include <stdlib.h>
#include <math.h>
#include <windows.h>
/*********************************************************************************
Function : uniform_random
Description : This is the worst random number routine in the known universe,
but I use it for portability. Feel free to replace it.
Parameter :
Return : random 0 乣 1
Call :
*********************************************************************************/
double WINAPI uniform_random( )
{
return (double) rand( ) / (double) RAND_MAX;
}
/*********************************************************************************
Function : gaussian_random
Description :
Parameter :
Return :
Call :
*********************************************************************************/
double WINAPI gaussian_random( )
{
static int next_gaussian = 0;
static double saved_gaussian_value;
double fac, rsq, v1, v2;
if (next_gaussian == 0)
{
do {
v1 = 2.0 * uniform_random() - 1.0;
v2 = 2.0 * uniform_random() - 1.0;
rsq = v1 * v1 + v2 * v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac = sqrt(-2.0*log(rsq)/rsq);
saved_gaussian_value = v1 * fac;
next_gaussian = 1;
return v2 * fac;
}
else
{
next_gaussian = 0;
return saved_gaussian_value;
}
}
/*********************************************************************************
Function : evaluate_gaussian
Description :
Parameter : val (Input) -
sigma (Input) -
Return :
Call :
*********************************************************************************/
double WINAPI evaluate_gaussian(double val, double sigma)
{
/* This private definition is used for portability */
static const double PI = 3.14159265358979323846;
return 1.0 / (sqrt(2.0*PI) * sigma) * exp(-0.5 * (val*val / (sigma*sigma)));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -