This is the mail archive of the libstdc++@sources.redhat.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

fear and loathing in SGI's random-number generator




To implement things like std::random_shuffle, the code in bits/stl_algo.h
calls __random_number, which in turn calls either the standard rand(),
or the non-standard-but-cooler lrand48(), depending on if __STL_NO_DRAND48
has been #define'd or not in stl_config.h.

These RNGs use different functions for seeding, however.  The first uses
srand(), the second uses srand48().  Seeding with one function has no
effect if you happen to be using the "other" flavor of generator.

Add to this the observations that:

    1)  Most people only know to call srand(),
    2)  Most modern systems will have the 48-bit generator available,
    3)  Nowhere in the implementation do we make any mention of the seeding
        functions, other than the the 'using' statement for srand(),

and you can see a problem shaping up.  Users who seed with srand() and use
random_shuffle() are going to be surprised when they get the same results
over and over even though the seeds being used are different.  Their only
recourse is to dig into the guts of the library to find the conditional
compilation using __STL_NO_DRAND48, and then use that in their own code
to conditionally compile srand or srand48.

Anybody have any thoughts as to alleviate this?  Maybe provide some
seed_random() wrapper around the seeders that performs the same conditional
compilation?  Surely somebody has used random_shuffle before and solved
this already?  What do they use at SGI?


Phil
(Yes, I did find this out the hard way.  :-)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]