randomnumbers.cpp

来自「一个十分简单有效的正态随机数产生程序, 相信就是你想要的哦.」· C++ 代码 · 共 26 行

CPP
26
字号
/* Simple generator for normally distributed random numbers */

#include <iostream>
#include <time.h>

using namespace std;

// Basic method (not quite correct, but fast and sufficient for EAs)
double GetNormalRand (double mean, double variance) 
{
	double zSum = 0.0;
	for (int i=0; i<12; i++) zSum += rand()/(double)RAND_MAX;
  zSum -= 6.0;
  return zSum*variance + mean;
}


void main(int argc, char **argv) 
{
	// Initialize the seed of the random number generator with the current time
	srand((unsigned)time(NULL));

	// Generate 20 quasi-normal distributed random numbers
	for (int i=0; i<20; i++) 
		cout << GetNormalRand(0,1) << endl;
}

⌨️ 快捷键说明

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