Reputation: 684
I'm trying to call rand
using dlfcn.h
(d.
I have this code, based on a project I'm working on. The core issue for me is that when used this way, it compiles and even runs, but its output is
cif: cif is OK
rand val: 16807
And the value of rand val
is 16807 every time, contrary to what rand
is supposed to do. What am I doing wrong here?
Upvotes: 0
Views: 66
Reputation: 179462
You only call rand()
once. If unseeded, rand()
is probably going to use a static seed, probably zero (library-dependent).
Import srand
too, and seed it with a reasonably random quantity - for example, you could do
timeval tv;
gettimeofday(&tv, NULL);
srand_ptr(tv.tv_sec ^ tv.tv_usec);
to seed it with the current time (seconds and microseconds).
Alternatively, on OS X, consider using arc4random
instead, which is automatically seeded and generally produces better random numbers.
Upvotes: 1