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]

Re: fear and loathing in SGI's random-number generator


Phil Edwards wrote:
> 
> 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?

IMHO those who realy need "random" random use thier own random generator
(random_shuffle has a version which get a random generator) or know about
srand48 (look into the code) those who just need "some" random generator
the deafult may be good.
anyway I think it'd be good to put srand48 into namespace std as srand
on platform which support it and those who realy use c++ eg. <cstdlib>
find that srand and don't have to care about it.
 
> Phil
> (Yes, I did find this out the hard way.  :-)

me too:-( but the worst is that random generator in standard algorithm
passed as value (!!!) and not by reference, when you've got a good
random generator is more difficult find out:-(

 -- Levente
 "The only thing worse than not knowing the truth is
  ruining the bliss of ignorance."

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