First, srand() isn't a random function; it sets up the starting point
of a pseudo-random sequence. And somewhat surprisingly, your
implementation of rand() seems to be returning a value based on the
previous state, and not on the newly calculated state, so that the first
value after a call to srand()
depends very much on the value passed to
srand()
. If you were to write:
srand( time( NULL ) );
rand();
std::cout << rand() << std::endl;
, I'm sure you'll see a lot more difference.
FWIW: I tried the following on both Windows and Linux:
int
main()
{
srand( time( NULL ) );
int r1 = rand();
std::cout << r1 << ' ' << rand() << std::endl;
return 0;
}
Invoked 10 times at a one second interval, I got:
16391 14979
16394 25727
16397 3708
16404 25205
16407 3185
16410 13933
16417 2662
16420 13411
16427 2139
with VC++ under Windows—you'll note the very low variance of the
first call to rand()
—and
1256800221 286343522
955907524 101665620
1731118607 991002476
1428701871 807009391
44395298 1688573463
817243457 1506183315
507034261 1310184381
1278902902 54648487
2049484769 942368151
1749966544 1833343137
with g++ under Windows; in this case, even the first value read is
relatively random.
If you need a good random generator, you'll probably have to use one
from Boost; the standard doesn't say much about what algorithm should be
used, and implementations have varied enormously in quality.