Mark,

In iomonad.c we have:

primFun(primGetRandomSeed) {            /* generate a random seed          */
    IOReturn(bigInt(clock()));
}

The clock() function returns the processor time used by the current
process, to the nearest clock tick. Thus the returned seed will 
recur quite frequently between successive invocations of Hugs.

Is there any good reason why clock() is used here? I can only speculate 
that a time(NULL) or gettimeofday(...) call was intended instead.

The first method, time(NULL), still seems to exhibit some non-random 
behaviour, as the returned times will be close together.

gettimeofday(...) gives improved results if we assign significance
to the microseconds, e.g.:

#include <sys/time.h>

primFun(primGetRandomSeed) {            /* generate a random seed          */
    struct timeval tv;
    gettimeofday(&tv, NULL);
    IOReturn(bigInt((tv.tv_usec << 12) + tv.tv_sec));
}

Certainly either of these methods yields better random numbers than
the current clock() implementation, and both are at least as portable
as clock().

        Matt


-- 
Matthew "Austin" Chapman
SysAdmin, Developer, Samba Team Member

Reply via email to