Facy random seed for qrand in kdelibs

Sebastian TrĂ¼g trueg at kde.org
Mon Aug 2 15:52:32 BST 2010


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 );
  }
}




More information about the kde-core-devel mailing list