portforwardpodcast portforwardpodcast - 5 months ago 54
C++ Question

Prevent ZMQ c++ from calling rand()

I am using ZMQ 4.1.2 on Ubuntu 10.04.5 LTS.

I have a c++ program that seeds srand() with a fixed number, and then calls rand() ~100k times and exists. I found that I was getting different random numbers when re-running the same program twice.

If I have a ZMQ socket open before I start my 100k draws it seems like the ZMQ library itself is calling rand() and messing up my repeatability.

this->context = new zmq::context_t(1);
this->socket = new zmq::socket_t(*this->context, ZMQ_PUB);
socket->connect("tcp://localhost:5556"); // offending line

All I need to do is omit calling socket->connect() and my calls to rand() behave deterministically.

Is this a bug (feature) in ZMQ? Or does this also happen with the underlying TCP socket?


You want to be using rand_r instead of rand so your usage doesn't conflict with other libraries using rand. e.g.

unsigned int seed = YOUR_INITIAL_SEED;

for (int x = 0; x < 100000; x++)
    unsigned int r = rand_r(&seed);