[Scottish] Auric Aorta ..

Paul Millar scottish at mailman.lug.org.uk
Thu Mar 20 12:41:00 2003


On Thu, 20 Mar 2003, Huard, Elise - D C&W Consultant wrote:
> wee question (to replace the pin-drop jokes :-) ) :
                                              ^^^^^^^^
Going a touch Picasso there ?  ;)

> program in C under Unix.
> Is there any way to get the time in tenth or hundredth of seconds ?
> Or to phrase it differently : i need a random number generator that won't
> give the same sequence of numbers every time that the seed is reinitialised
> in the same second (by 2 different users)
> srand(time(NULL)) 
> and then rand()
> doesn't work.
> Or a different kind of seed ?  Suggestions are welcome (should be readily
> available in your standard Unix system)

Well, getting tenth or hundredth of seconds doesn't solve your problem;
you just make the race-condition finer-grained.  Two users could still get
the same seed, its just less likely.  For example, a user starts two
instances of the program (deliberate or accident), both wait on a common
synchronising signal (network packet?) you wouldn't believe how often
these things happen ....  So when it *does* happen (and presumably
something nasty happens as a consequence), it will appear more bizarre and
take longer to debug what when wrong.

The way to make sure two users don't get the same seed is to have some
concept of a seed-server.  The simplest thing would be a file somewhere
with an integer stored inside.  Suitable file-locking should prevent
multiple people accessing the file at the same time and if all the
seed-server-clients know to increment the integer, your fine.  More
sophisticated solutions are possible, but that's probably good enough if
you have full control of the clients.

The other alternative is not to use rand().  The implementations tend to
be linear congruential generators, which have a checkered history.  The
GNU ones used in Linux are ok, AFAIK; although they probably suffer from
the same problems of correlations in higher dimensions discussed in:
  http://lib-www.lanl.gov/numerical/bookcpdf/c7-1.pdf
(yes, I know it NRC again, but its a reasonable introduction ;)

Alternative is to use /dev/random (or /dev/urandom depending on what you
want).  I don't know if this is on a standard Unix system, but what *is* a
"standard Unix system" anyway? ;)

HTH

Paul.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
Particle Physics (Theory & Experimental) Groups                Dr Paul Millar 
Department of Physics and Astronomy                     paulm@astro.gla.ac.uk
University of Glasgow                                 paulm@physics.gla.ac.uk
Glasgow, G12 8QQ, Scotland             http://www.astro.gla.ac.uk/users/paulm 
+44 (0)141 330 4717        A54C A9FC 6A77 1664 2E4E  90E3 FFD2 704B BF0F 03E9
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --