public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* Random Number Seed
@ 2004-03-01 16:03 Przemyslaw Sliwa
  2004-03-01 16:53 ` Frederick Joseph Ross
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Przemyslaw Sliwa @ 2004-03-01 16:03 UTC (permalink / raw)
  To: gsl-discuss

Hi,

I have a question:
When one wants to use the random number seed different than the default one (equals to 0) one can use the macro GSL_RNG_SEED=seed from the command line. I would like to use the system time as the seed and have no idea how one can use the it from the command line. Therefore I want to use the function clock() in my C program. Could you help me how the seed can be initialized from the function claock() within my c program?

Thank you for help,

Pshem




--------------------------------------------------
Deutsche Bank AG
Risk Controlling
Credit Risk Engineering & Implementation
            /  Daily Credit Risk

Tel.:  (+44 20) 754 59676 London
Fax:   (+44 20) 754  71060 London

----------------------------------------------------


--

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2004-03-01 16:03 Random Number Seed Przemyslaw Sliwa
@ 2004-03-01 16:53 ` Frederick Joseph Ross
  2004-03-01 17:55 ` Robert G. Brown
  2004-03-04 17:42 ` Brian Gough
  2 siblings, 0 replies; 9+ messages in thread
From: Frederick Joseph Ross @ 2004-03-01 16:53 UTC (permalink / raw)
  To: Przemyslaw Sliwa; +Cc: gsl-discuss

The function you're after (at least on POSIX systems) is time in time.h 
(try man 2 time if you're on a Linux or BSD machine).  I nearly always 
seed my generator this way at the start of a program.  For instance,

#include <gsl/gsl_rng.h>
#include <time.h>                                                                                  
                                                                                                                                     
int main(int argc, char *argv[])
{
	const gsl_rng_type *T = gsl_rng_mt19937;
	gsl_rng *r = gsl_rng_alloc(T);

	gsl_rng_set(r, (unsigned long) time(NULL))

	...

}
	
Hope this helps.

Fred Ross
High Energy Physics Laboratory
University of Virginia

> Hi,
> 
> I have a question:
> When one wants to use the random number seed different than the default one (equals to 0) one can use the macro GSL_RNG_SEED=seed from the command line. I would like to use the system time as the seed and have no idea how one can use the it from the command line. Therefore I want to use the function clock() in my C program. Could you help me how the seed can be initialized from the function claock() within my c program?
> 
> Thank you for help,
> 
> Pshem
> 
> 
> 
> 
> --------------------------------------------------
> Deutsche Bank AG
> Risk Controlling
> Credit Risk Engineering & Implementation
>             /  Daily Credit Risk
> 
> Tel.:  (+44 20) 754 59676 London
> Fax:   (+44 20) 754  71060 London
> 
> ----------------------------------------------------
> 
> 
> --
> 
> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2004-03-01 16:03 Random Number Seed 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
  2004-03-04 17:42 ` Brian Gough
  2 siblings, 2 replies; 9+ messages in thread
From: Robert G. Brown @ 2004-03-01 17:55 UTC (permalink / raw)
  To: Przemyslaw Sliwa; +Cc: gsl-discuss

On Mon, 1 Mar 2004, Przemyslaw Sliwa wrote:

> Hi,
> 
> I have a question:
> When one wants to use the random number seed different than the default one (equals to 0) one can use the macro GSL_RNG_SEED=seed from the command line. I would like to use the system time as the seed and have no idea how one can use the it from the command line. Therefore I want to use the function clock() in my C program. Could you help me how the seed can be initialized from the function claock() within my c program?

This is getting to be a faq.  Here is a short discursion on seeds yet
again.

Depending on the rng chosen, using the clock as a seed ranges from a
maybe-safe bad idea to a really BAD bad idea.  Obviously the seeds on
all jobs started in (say) any given hour will have substantial bit-level
correlations.  Whether or not those bit-level correlations will cause
supposedly "independent" jobs started with nearby seeds to exhibit
unexpected correlations depends in part on the quality of the rng
selected, but LOTS of the GSL rngs are not terriby high quality and
would be likely to exhibit the problem.

Seeding by hand can also be problematic as humans have a hard time
selecting random unsigned long integers from the full range of available
values.

The "best" solution (in my opinion) for seeding a rng to get unique rng
series in disparate computations (so one can, for example, apply
statistics safely to results from the computations under the assumption
that those results are "independent, identically distributed" numbers
according to the requirements of statistics and the central limit
theorem) is to do the following:

  a) Use an rng with a very, very, very,...very long period.  The period
really should be long enough that all of your samplings from the rng are
"unlikely" to overlap.

  b) Use a "high quality" rng, one that passes the Diehard suite or most
of the NIST/FPE suite of tests of randomness.  

The default GSL rng, mt19937, is a very good choice wrt both a) and b).
It has a period of 2^19937, which is yes, a very large number and has
passed the diehard tests.  It is also pretty fast -- one of the faster
generators in the GSL suite.

  c) Seed the generator from /dev/random when it is available.
/dev/random is slow and unsuitable for monte carlo sampling in most
cases, but it is highly "unpredictable" and appears to do well on
bit-level randomness tests.  It is almost certainly adequate and may
even be ideal.  Note that EVEN mt19937 had problems with bit
correlations caused by certain seeds -- the current version is
supposedly fixed but it still cannot hurt at all to use the most random
seed you have available.

  d) If you DO want to ensure that all your samplings drawn from each
seed are unique, record the seeds and use them to label your answers in
such a way that IF by any miracle you get two seeds that are identical,
the answer derived from those two runs is only counted once.  In most
cases this will make no observable difference in the answer, of course,
if one is pulling seeds from bitlevel-random unsigned long ints, but is
still a good practice.

  e) Only if /dev/random is not available consider using the clock.  In
that case you can use a bit of common sense to determine whether or not
to take extra measures.  If you're writing a game, don't bother.  If
you're doing simulations, you MIGHT want to use the clock to reseed one
(good) rng, and use the first rng to determine e.g.  a bitshuffling or
other "randomization" of the original seed to create a new, less
obviously correlated seed for the second (better) rng.  I don't have an
explicit theoretical foundation for this (although there may be one) but
intuitively doing this in two stages with good rngs will break up
bitlevel correlations in the second while diluting overall seed-based
correlation by something like the product of the available phase spaces.

A code snippet for seeding from /dev/random (with fallback from the
clock) is included below.  It basically returns an unsigned long
integer with at least some of its bits set by the faster usec scale clock in
gettimeofday.  If you prefer, you could only use the seconds portion of this.
It is important to note that the addition it uses has a distinct nonzero 
probability of returning the same seed but is generally more "random"; using 
seconds alone is very strongly correlated (and will OFTEN return the same 
seed value if multiple jobs are started per second or on a cluster where 
there is a bit of clock drift).

   rgb

-- 
Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb@phy.duke.edu

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

}


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2004-03-01 17:55 ` Robert G. Brown
@ 2004-03-01 20:06   ` Daniel T Konkle
  2005-02-21 11:55   ` Olaf Lenz
  1 sibling, 0 replies; 9+ messages in thread
From: Daniel T Konkle @ 2004-03-01 20:06 UTC (permalink / raw)
  To: gsl-discuss


http://www.onlamp.com/pub/a/onlamp/excerpt/PUIS3_chap16/index4.html?page=3

http://www.onlamp.com/pub/a/onlamp/excerpt/PUIS3_chap16/index4.html?page=2

I've not tested it but this perl script at this link to compute a random 
number seed seems to be better than using the time.

Danny

At 11:55 AM 3/1/2004, Robert G. Brown wrote:
>On Mon, 1 Mar 2004, Przemyslaw Sliwa wrote:
>
> > Hi,
> >
> > I have a question:
> > When one wants to use the random number seed different than the default 
> one (equals to 0) one can use the macro GSL_RNG_SEED=seed from the 
> command line. I would like to use the system time as the seed and have no 
> idea how one can use the it from the command line. Therefore I want to 
> use the function clock() in my C program. Could you help me how the seed 
> can be initialized from the function claock() within my c program?
>
>This is getting to be a faq.  Here is a short discursion on seeds yet
>again.
>
>Depending on the rng chosen, using the clock as a seed ranges from a
>maybe-safe bad idea to a really BAD bad idea.  Obviously the seeds on
>all jobs started in (say) any given hour will have substantial bit-level
>correlations.  Whether or not those bit-level correlations will cause
>supposedly "independent" jobs started with nearby seeds to exhibit
>unexpected correlations depends in part on the quality of the rng
>selected, but LOTS of the GSL rngs are not terriby high quality and
>would be likely to exhibit the problem.
>
>Seeding by hand can also be problematic as humans have a hard time
>selecting random unsigned long integers from the full range of available
>values.
>
>The "best" solution (in my opinion) for seeding a rng to get unique rng
>series in disparate computations (so one can, for example, apply
>statistics safely to results from the computations under the assumption
>that those results are "independent, identically distributed" numbers
>according to the requirements of statistics and the central limit
>theorem) is to do the following:
>
>   a) Use an rng with a very, very, very,...very long period.  The period
>really should be long enough that all of your samplings from the rng are
>"unlikely" to overlap.
>
>   b) Use a "high quality" rng, one that passes the Diehard suite or most
>of the NIST/FPE suite of tests of randomness.
>
>The default GSL rng, mt19937, is a very good choice wrt both a) and b).
>It has a period of 2^19937, which is yes, a very large number and has
>passed the diehard tests.  It is also pretty fast -- one of the faster
>generators in the GSL suite.
>
>   c) Seed the generator from /dev/random when it is available.
>/dev/random is slow and unsuitable for monte carlo sampling in most
>cases, but it is highly "unpredictable" and appears to do well on
>bit-level randomness tests.  It is almost certainly adequate and may
>even be ideal.  Note that EVEN mt19937 had problems with bit
>correlations caused by certain seeds -- the current version is
>supposedly fixed but it still cannot hurt at all to use the most random
>seed you have available.
>
>   d) If you DO want to ensure that all your samplings drawn from each
>seed are unique, record the seeds and use them to label your answers in
>such a way that IF by any miracle you get two seeds that are identical,
>the answer derived from those two runs is only counted once.  In most
>cases this will make no observable difference in the answer, of course,
>if one is pulling seeds from bitlevel-random unsigned long ints, but is
>still a good practice.
>
>   e) Only if /dev/random is not available consider using the clock.  In
>that case you can use a bit of common sense to determine whether or not
>to take extra measures.  If you're writing a game, don't bother.  If
>you're doing simulations, you MIGHT want to use the clock to reseed one
>(good) rng, and use the first rng to determine e.g.  a bitshuffling or
>other "randomization" of the original seed to create a new, less
>obviously correlated seed for the second (better) rng.  I don't have an
>explicit theoretical foundation for this (although there may be one) but
>intuitively doing this in two stages with good rngs will break up
>bitlevel correlations in the second while diluting overall seed-based
>correlation by something like the product of the available phase spaces.
>
>A code snippet for seeding from /dev/random (with fallback from the
>clock) is included below.  It basically returns an unsigned long
>integer with at least some of its bits set by the faster usec scale clock in
>gettimeofday.  If you prefer, you could only use the seconds portion of this.
>It is important to note that the addition it uses has a distinct nonzero
>probability of returning the same seed but is generally more "random"; using
>seconds alone is very strongly correlated (and will OFTEN return the same
>seed value if multiple jobs are started per second or on a cluster where
>there is a bit of clock drift).
>
>    rgb
>
>--
>Robert G. Brown                        http://www.phy.duke.edu/~rgb/
>Duke University Dept. of Physics, Box 90305
>Durham, N.C. 27708-0305
>Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb@phy.duke.edu
>
>#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);
>
>}

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2004-03-01 16:03 Random Number Seed Przemyslaw Sliwa
  2004-03-01 16:53 ` Frederick Joseph Ross
  2004-03-01 17:55 ` Robert G. Brown
@ 2004-03-04 17:42 ` Brian Gough
  2 siblings, 0 replies; 9+ messages in thread
From: Brian Gough @ 2004-03-04 17:42 UTC (permalink / raw)
  To: gsl-discuss

Przemyslaw Sliwa writes:
 > I have a question: When one wants to use the random number seed
 > different than the default one (equals to 0) one can use the macro
 > GSL_RNG_SEED=seed from the command line. I would like to use the
 > system time as the seed and have no idea how one can use the it
 > from the command line. Therefore I want to use the function clock()
 > in my C program. Could you help me how the seed can be initialized
 > from the function claock() within my c program?

For real work be sure to record any non-deterministic seed values in
case they are needed for later investigation.

-- 
Brian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2004-03-01 17:55 ` Robert G. Brown
  2004-03-01 20:06   ` Daniel T Konkle
@ 2005-02-21 11:55   ` Olaf Lenz
  2005-02-21 12:03     ` Jerome BENOIT
  2005-02-21 12:47     ` Robert G. Brown
  1 sibling, 2 replies; 9+ messages in thread
From: Olaf Lenz @ 2005-02-21 11:55 UTC (permalink / raw)
  To: Robert G. Brown; +Cc: Przemyslaw Sliwa, gsl-discuss

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2005-02-21 11:55   ` Olaf Lenz
@ 2005-02-21 12:03     ` Jerome BENOIT
  2005-02-21 12:47     ` Robert G. Brown
  1 sibling, 0 replies; 9+ messages in thread
From: Jerome BENOIT @ 2005-02-21 12:03 UTC (permalink / raw)
  Cc: gsl-discuss

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2005-02-21 11:55   ` Olaf Lenz
  2005-02-21 12:03     ` Jerome BENOIT
@ 2005-02-21 12:47     ` Robert G. Brown
  2005-02-22  8:19       ` Olaf Lenz
  1 sibling, 1 reply; 9+ messages in thread
From: Robert G. Brown @ 2005-02-21 12:47 UTC (permalink / raw)
  To: Olaf Lenz; +Cc: Przemyslaw Sliwa, gsl-discuss

On Mon, 21 Feb 2005, 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.
> 
> 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:

My bad, sorry.  I was assuming that the system itself wasn't broken
here, so that /dev/random might be absent but wouldn't be broken.  In
the man page (and to my experience):

       When read, the /dev/random device will only return random bytes
       within the estimated number of bits of noise in the entropy pool.
       /dev/random should be suitable for uses that need very high
       quality randomness such as one-time pad or key generation.  When
       the entropy pool is empty, reads from /dev/random will block
       until additional environmental noise is gathered.

So if you can open and try to read /dev/random the device blocks the
read until it successfully returns an integer (which can actually take a
long time if you are asking for a lot of rands as it has to generate
sufficient entropy).

So either this behavior is somehow implementation dependent on systems
with /dev/random, or your system has a broken /dev/random in the kernel,
or the system has a regular null file called "/dev/random" in the /dev
directory instead of the kernel-linked device.  In this latter case
(which I did not anticipate but yes, which is possible) you'd get the
behavior you describe, but I would consider the system "broken" and I'd
think that you want to know it instead of continuing.  Especially since
putting a file in place of /dev/random would make a system vulnerable to
various cryptographic attacks, one would think.  So much so that I'd
"suspect" an attack if I found a regular file there on a system properly
installed from a well-supported linux distribution.

So one might want to replace the "else" for fread with an error message
and punt unless your system has a different man page than mine and
doesn't state that /dev/random (unlike /dev/urandom) should block until
it returns.

    rgb

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

-- 
Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb@phy.duke.edu


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Random Number Seed
  2005-02-21 12:47     ` Robert G. Brown
@ 2005-02-22  8:19       ` Olaf Lenz
  0 siblings, 0 replies; 9+ messages in thread
From: Olaf Lenz @ 2005-02-22  8:19 UTC (permalink / raw)
  To: GSL Discussion List

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

Robert G. Brown wrote:
|> My bad, sorry.  I was assuming that the system itself wasn't broken
|> here, so that /dev/random might be absent but wouldn't be broken.
[clip]

I do not blame you for the code! I would have probably written the same
code myself.

I was thinking along the same lines as you - that our system is broken.
It turned out that the problem is the queuing system "condor" that we
use. Condor wraps the system calls of the executable with its own
versions, and these lead to the strange behaviour described: the fopen()
call succeeds, the fread() returns 0 and does not set the seed, and
feof() is set.

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

iD8DBQFCGur4tQ3riQ3oo/oRAmJSAJwNt2sWgY8YpQIZS+pICMpTWeqaGgCgoTCR
sNVXLTsHH88RCWoRbY3Q1Gc=
=Hweq
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-02-22  8:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-01 16:03 Random Number Seed 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
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

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