public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Olaf Lenz <olenz@Physik.Uni-Bielefeld.DE>
To: "Robert G. Brown" <rgb@phy.duke.edu>
Cc: Przemyslaw Sliwa <przemyslaw.sliwa@db.com>,
	gsl-discuss@sources.redhat.com
Subject: Re: Random Number Seed
Date: Mon, 21 Feb 2005 11:55:00 -0000	[thread overview]
Message-ID: <4219CC24.90200@physik.uni-bielefeld.de> (raw)
In-Reply-To: <Pine.LNX.4.44.0403011221340.20444-100000@ganesh>

-----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.

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-----

  parent reply	other threads:[~2005-02-21 11:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-01 16:03 Przemyslaw Sliwa
2004-03-01 16:53 ` Frederick Joseph Ross
2004-03-01 17:55 ` Robert G. Brown
2004-03-01 20:06   ` Daniel T Konkle
2005-02-21 11:55   ` Olaf Lenz [this message]
2005-02-21 12:03     ` Jerome BENOIT
2005-02-21 12:47     ` Robert G. Brown
2005-02-22  8:19       ` Olaf Lenz
2004-03-04 17:42 ` Brian Gough

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4219CC24.90200@physik.uni-bielefeld.de \
    --to=olenz@physik.uni-bielefeld.de \
    --cc=gsl-discuss@sources.redhat.com \
    --cc=przemyslaw.sliwa@db.com \
    --cc=rgb@phy.duke.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).