📄 randmaker.cpp
字号:
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include "RandMaker.h"
#define PI 3.1415926535897932384626433832795
void RandMaker::RandInit()
{
srand(GetTickCount());
}
double RandMaker::AverageRand(double min, double max)
{
const double precise = 10000.0;
int minInteger = (int)(min*precise);
int maxInteger = (int)(max*precise);
int randInteger = rand() * rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger / precise;
}
static double Normal(double x, double miu, double sigma)
{
return 1 / (sqrt(2 * PI) * sigma) * exp(-(x - miu) * (x - miu) / (2 * sigma * sigma));
}
/* the pfn means the density of the random number */
double RandMaker::NormalRand(double min, double max, double miu, double sigma)
{
double x, y, dScope;
do
{
x = AverageRand(min, max);
y = Normal(x, miu, sigma);
dScope = AverageRand(0, Normal(miu, miu, sigma));
} while(dScope > y);
return x;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -