Facy random seed for qrand in kdelibs

George Kiagiadakis kiagiadakis.george at gmail.com
Mon Aug 2 15:53:39 BST 2010


On Mon, Aug 2, 2010 at 5:52 PM, Sebastian Trüg <trueg at kde.org> 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?
>
> Cheers,
> Sebastian
>
> 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 is the code from KRandom::random(), already in kdelibs.




More information about the kde-core-devel mailing list