Facy random seed for qrand in kdelibs

Thiago Macieira thiago at kde.org
Mon Aug 2 16:23:40 BST 2010


On Monday 2. August 2010 11.52.32 Sebastian TrĂ¼g wrote:
> one of kdepim's Akonadi resources defines the method below to create a
> proper random seed for qrand. IMHO we should put this in kdelibs
> directly so all apps can benefit from it. Maybe KGlobal is the right
> place for it?
> 
> Any objections?

Actually, after reading the code, yes, unfortunately, I do.

> static void initRandomSeed()
> {
>   static bool init = false;
>   if ( !init ) {
>     unsigned int seed;
>     init = true;
>     int fd = KDE_open( "/dev/urandom", O_RDONLY );
>     if ( fd < 0 || ::read( fd, &seed, sizeof( seed ) ) != sizeof( seed ) )
> { // No /dev/urandom... try something else.
>       srand( getpid() );
>       seed = rand() + time( 0 );
>     }
> 
>     if ( fd >= 0 )
>       close( fd );
> 
>     qsrand( seed );
>   }
> }

This implementation isn't correct.

You must set "init" as the last thing, otherwise other threads might skip over 
the initialisation and try to get the data before the seeding is complete.

Using a "bool" isn't enough for this, since the compiler might reorder the 
instructions. You should use at least a volatile, but a QBasicAtomicInt is 
preferrable.

But the big problem is that qsrand and qrand are thread-specific. You must run 
the above code once per started thread. Your code is global (once per 
application). So it won't work.

The implementation in Qt 4.6.4 and 4.7.0 is preferred. If required, we can 
cherry-pick f28a27987fa4e1b2faa1c57188c128afb735f70a and 
2ef8b92ececbf9d33d7c0b44f46c7c975fb0fdaa into the kde-qt 4.6.3-patched and 
master branches.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20100802/a4b1478b/attachment.sig>


More information about the kde-core-devel mailing list