如何保存/检索MT19937,以便重复序列
How to save/retrieve mt19937 so that the sequence is repeated?
这是我的尝试
using namespace std;
int main()
{
mt19937 mt(time(0));
cout << mt() << endl;
cout << "----" << endl;
std::ofstream ofs;
ofs.open("/path/save", ios_base::app | ifstream::binary);
ofs << mt;
ofs.close();
cout << mt() << endl;
cout << "----" << endl;
std::ifstream ifs;
ifs.open("/path/save", ios::in | ifstream::binary);
ifs >> mt;
ifs.close();
cout << mt() << endl;
return 0;
}
这是可能的输出
1442642936
----
1503923883
----
3268552048
我希望最后两个数字相同。显然,我没有写和/或阅读我的MT19937。您可以帮助修复此代码吗?
打开文件写作时,您将附加到现有文件。当您重新阅读时,您会从一开始就阅读。
假设您不想保留现有内容,请将打开的呼叫更改为
ofs.open("/path/save", ios_base::trunc | ifstream::binary);
使用trunc
标志而不是app
将截断现有文件,因此,当您重新打开它时,您将在刚刚编写的数据中读取而不是已经存在的旧数据。
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 使用std::mt19937从字符串中返回一个随机单词
- 是否可以从格式字符串中检索"width"
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 如何使用要传递给 mt19937 的可选随机种子参数设计函数
- <random>在实践中应该实际使用哪个随机数引擎? std::mt19937?
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- 如何在Qt中从数据库中检索二进制数据?
- 如何从C++代码中检索 QML 的文本字段中的文本?
- 从 opencv c++ 中的矢量中检索固定的帧数
- 如何在不等待检索的情况下获取C++中的内存位置?
- 如何在 QTreeWidget 中检索特定项目的 mimeData?
- std::mt19937 in Rcpp
- C++从字符串中检索几行
- 在数据库中插入和检索矩阵
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 使用 const char* 键映射 C++ 检索空值
- 打印从SQLite3数据库检索的数据C++与命令行中相同的方式
- 如何检索指向在单独线程上运行的函数的移动指针?
- 如何保存/检索MT19937,以便重复序列