This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Random Number Seed


Hello,



Olaf Lenz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

A few months ago, you suggested the following code snippet for seeding
the RNG from /dev/random:

- -------------------------------------------------
#include <stdio.h>
#include <sys/time.h>

unsigned long int random_seed()
{

~ unsigned int seed;
~ struct timeval tv;
~ FILE *devrandom;

~ if ((devrandom = fopen("/dev/random","r")) == NULL) {
~   gettimeofday(&tv,0);
~   seed = tv.tv_sec + tv.tv_usec;
~   if(verbose == D_SEED) printf("Got seed %u from gettimeofday()\n",seed);
~ } else {
~   fread(&seed,sizeof(seed),1,devrandom);
~   if(verbose == D_SEED) printf("Got seed %u from /dev/random\n",seed);
~   fclose(devrandom);
~ }

~ return(seed);
}
- -------------------------------------------------

I've used the code for quite a while now and only today I noticed a big
problem with it. The code tests, if /dev/random can be opened, but it
does NOT test if the fread has actually read any number.

On recent kernel (2.4.x,2.6.x),
you can easily know if there are available random numbers
by reading /proc/sys/kernel/random/entropy_avail
(see the Documentation distributed with the kernel source).
Second, you can read /dev/urandom Third, if you have an apropriate hard ware,
you can use the rng-tools to feed with true random number your /dev/random.



hth, Jerome



In my case, this resulted in the fact that the seed was not seeded at all and all processes used the same seed.... P-(

So to all who have been using the code, I would recommend to check their
results. For the future, I would recommend to use the following code:

- -------------------------------------------------
#include <stdio.h>
#include <sys/time.h>

unsigned long int random_seed()
{

~ unsigned int seed;
~ struct timeval tv;
~ FILE *devrandom;

~ if ((devrandom = fopen("/dev/random","r")) == NULL) {
~   gettimeofday(&tv,0);
~   seed = tv.tv_sec + tv.tv_usec;
~   if(verbose == D_SEED) printf("Got seed %u from gettimeofday()\n",seed);
~ } else {
~   if (fread(&seed,sizeof(seed),1,devrandom) == 1) {
~     if(verbose == D_SEED) printf("Got seed %u from /dev/random\n",seed);
~     fclose(devrandom);
~   } else {
~     gettimeofday(&tv,0);
~     seed = tv.tv_sec + tv.tv_usec;
~     if(verbose == D_SEED) printf("Got seed %u from
gettimeofday()\n",seed);

~   }
~ }

~ return(seed);

}
- -------------------------------------------------

Cheers
    Olaf
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCGcwjtQ3riQ3oo/oRAsjeAKC3CIz3kxxt/ZJUiuYzemIU1IqVdgCffoYW
vXr8SEcXH69ulMzTfBwWuHw=
=2RKb
-----END PGP SIGNATURE-----


-- Dr. Jerome BENOIT room A2-26 Complexo Interdisciplinar da U. L. Av. Prof. Gama Pinto, 2 P-1649-003 Lisboa, Portugal email: jgmbenoit@wanadoo.fr or benoit@cii.fc.ul.pt -- If you are convinced by the necessity of a European research initiative, please visit http://fer.apinc.org


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