如何在C 中每个应用程序一次播种MT19937并多次使用它
How to seed mt19937 once per app in C++ and use it multiple times?
我可以在简单的应用中获得MT19937 RNG。现在,我试图将其播种一次,并在需要时多次使用它。这是我的代码。我遇到的错误是在GeneraterAndomNumber中 - " Gen:未宣布的标识符"。
main.cpp
#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;
}
}
General.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);
General.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);
}
在这里,
mt19937 gen; // gen is an object of class mt19937! not a function
void CallOncePerApp()
{
// Error C2064 term does not evaluate to a function taking 1 arguments
gen(rd); // **** therefore this line is wrong!
}
还需要在" eneral.cpp"文件中包含标头文件。
相关文章:
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在C++中一次将矢量值写入多个文件
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 在一次迭代中从 txt 文件中读取多行
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 高级选择排序 - 在一次迭代中搜索两个元素
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何仅使用一次固定<<设置精度(2)?或者至少恢复到默认行为?
- 什么是仅调用一次并调用参数的控制台应用
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 为什么我的信号处理程序只执行一次?
- 如何使用C++一次读取整个二进制文件
- 花括号使循环运行一次?
- 从子菜单返回后,正确的输入至少进入验证循环一次
- 如何在C 中每个应用程序一次播种MT19937并多次使用它