1.标准正态分布函数
#include <time.h>
#include <stdlib.h>
#define rd (rand() / (RAND_MAX + 1.0))
double randn(void)
{
static int flag = 0;
if (flag == 0)
{
srand((unsigned)time(NULL)); // 设置随机数种子为当前时间的秒数
flag = 1;
}
return rd + rd + rd + rd + rd + rd + rd + rd + rd + rd + rd + rd - 6.0;
}
2.服从0-1分布的函数
#include <time.h>
#include <stdlib.h>
double uniform(void)
{
static int seed;
static int flag = 0;
if (flag == 0)
{
flag = 1;
srand(time(NULL)); // 设置随机数种子为当前时间的秒数
seed = rand();
}
seed = 2045 * seed + 1;
seed = seed - (seed / 1048576) * 1048576;
return (seed) / 1048576.0;
}