防止ZMQ c++调用rand()
Prevent ZMQ c++ from calling rand()
我在Ubuntu 10.04.5 LTS上使用ZMQ 4.1.2。
我有一个c++程序,它用一个固定的数字播种rand(),然后调用rand() ~100k次并存在。我发现当我两次重新运行同一个程序时,我得到的是不同的随机数。
如果我有一个ZMQ套接字打开之前,我开始我的100k绘制,似乎ZMQ库本身调用rand()和搞乱我的可重复性。
this->context = new zmq::context_t(1);
this->socket = new zmq::socket_t(*this->context, ZMQ_PUB);
socket->connect("tcp://localhost:5556"); // offending line
我所需要做的就是省略调用socket->connect(),并且我对rand()的调用行为是确定性的。
这是ZMQ中的一个bug(特性)吗?或者这也发生在底层TCP套接字上?
您希望使用rand_r
而不是rand
,以便您的使用不会与使用rand的其他库冲突。例如
unsigned int seed = YOUR_INITIAL_SEED;
for (int x = 0; x < 100000; x++)
{
unsigned int r = rand_r(&seed);
DoMyThing(r);
}
https://linux.die.net/man/3/rand 相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 在具有相同种子的同一程序中对srand()的第二次调用不会为连续的rand()生成相同的值
- Rand() 在每次函数调用时返回相同的值
- 如何直接获取rand()序列中的第n个数字,而不必调用rand(()n次
- 如何调用一个变量(包含rand()函数)并得到不同的数字
- rand()在通过函数调用时生成相同的随机数集(即使在使用srand(time(NULL)进行种子设定之后)
- 为什么在使用rand生成随机数之前需要调用srand
- Rand() 是相同的数字生成器,即使我调用了 srand(time(NULL))
- CryptGenRandom输出与rand()调用不同
- 防止ZMQ c++调用rand()
- Rand()在单个函数中调用时返回相同的值