[Mesa-dev] [PATCH 1/5] util/disk_cache: seed rand

Alan Swanson reiver at improbability.net
Wed Mar 15 17:23:25 UTC 2017


On Tue, 2017-03-14 at 12:34 +0200, Grazvydas Ignotas wrote:
> It's not a good practice to srand() in libraries [1]. Not sure what
> to
> suggest though, maybe rand_r() but "POSIX.1-2008 marks rand_r() as
> obsolete". Maybe somebody else (Emil?) has some ideas?
> 
> [1] http://stackoverflow.com/questions/14883035/problems-of-using-sra
> nd-in-libraries

You just need to ensure it's (re)seeded with something unique to avoid
reusing the same generated sequence over different processes or threads
that have called srand(time(NULL)) within the same second due to the
second granularity of time().

  srand(time(NULL) + getpid() + clock());

Would be pretty unique. Don't think there is anyway to determine if
srand has been previously called so calling it once only in a library
is the only option.

-- 
Alan.


More information about the mesa-dev mailing list