Email List: Xaustin-review-lX
[All Lists]

Re: Defect in XCU awk (starting random number generators)

To: yyyyyyyyyyyyyyy@xxxxxxxxxxxxx
Subject: Re: Defect in XCU awk (starting random number generators)
From: Joe Gwinn <yyyyyyyy@xxxxxxxxxxx>
Date: Fri, 16 Apr 2004 15:00:06 -0400
References: <20040416151822.GA3268@squonk>
Comment below.

At 4:18 PM +0100 4/16/04, Geoff Clare wrote:
>@ page 167 line 6364 section awk comment [gwc awk srand time]
>
>Problem:
>
>Defect code :  3. Clarification required
>
>The description of the srand() function for awk says it uses the
>"time of day" if no argument is passed to srand().
>
>What does this mean exactly?  In particular:
>
>    1. Does it relate to the time value returned by the function
>    gettimeofday() in XSH?  If so, is it just the seconds or the
>    fractional part as well?
>
>    2. Or is it supposed to imply a value with a cycle of 24 hours?
>
>All the implementations I have tried use just the seconds from the
>time returned by gettimeofday() (i.e. the return value of time()).

The time is used only because it's always available and it always varies; the 
exact value isn't important.  One could as well use horserace betting figures, 
if they were easily had.


>I think a value with a cycle of 24 hours would be a bad idea and
>the specification should make it clear that this is not allowed.
>(Consider the effect on a cron job which is started at the same time
>each day.)

Given that the entire point of using the time is to ensure randomness of 
starting point, one second resolution seems like a probable source of trouble 
to me.  A better approach may be to use the nanoseconds longword instead.  If 
the fact that this will repeat once a second is a problem, a traditional 
alternative is to add the seconds-since-the-epoch longword to the nanoseconds 
longword, ignoring overflows, and use the resulting 32-bit sum as the random 
number generator's seed.  

In some systems, to ensure that on system startup the random number sequences 
are unlikely to start up in synchronism, a standard trick is to also add in the 
machine's MAC address and/or PID/TID, etc.

Joe Gwinn

<Prev in Thread] Current Thread [Next in Thread>