我可以在一个简单的应用程序中获得 mt19937 rng 种子。现在,我试图为每个应用程序播种一次,并在需要时多次使用它。这是我的代码。我得到的错误在 GenerateRandomNumber - “gen: undeclared identifier”中。
主文件
#include <iostream>
#include "general.h"
int main() {
CallOncePerApp();
// Loop for output testing
// Ultimately, GenerateRandomNumber can be called from anywhere in any CPP file
for (int i=0;i<10;i++) {
int dwGen = GenerateRandomNumber(1, 1000);
cout << dwGen; // print the raw output of the generator.
cout << endl;
}
}
一般.h:
#include <random>
using namespace std;
extern random_device rd;
extern void CallOncePerApp();
extern mt19937 gen;
extern unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax);
一般.cpp:
#include <random>
using namespace std;
random_device rd;
mt19937 gen;
void CallOncePerApp()
{
// Error C2064 term does not evaluate to a function taking 1 arguments
gen(rd); // Perform the seed once per app
}
unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax)
{
uniform_int_distribution <int> dist(dwMin, dwMax); // distribute results between dwMin and dwMax inclusive.
return dist(gen);
}