关于套接字:防止ZMQ c ++调用rand()

Prevent ZMQ c++ from calling rand()

我在Ubuntu 10.04.5 LTS上使用ZMQ 4.1.2。

我有一个C ++程序,该种子以固定的数量播种srand(),然后调用rand()?100k次并存在。 我发现重新运行同一程序两次时得到的随机数有所不同。

如果我在开始100k绘制之前打开了ZMQ套接字,则好像ZMQ库本身正在调用rand()并弄乱了我的可重复性。

1
2
3
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中的错误(功能)吗? 还是底层的TCP套接字也会发生这种情况?


您希望使用rand_r而不是rand,因此您的用法不会与使用rand的其他库冲突。
例如

1
2
3
4
5
6
7
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