Matt Chapman wrote:
> 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().
Thanks for your comments, Matt. Mark Jones is still helping with Hugs
project, but only on a consultancy capacity. The quickest way get a
reply is to mail to hugs-bugs or hugs-users (which I see you've done).
The simple answer is that yes, your right the random seed is not
very random, if there is no user/external interaction before the
seed is generated. The next generation Hugs uses a stronger mechanism,
sharing the code from GHC.
mkStdRNG :: Integer -> IO StdGen
mkStdRNG o = do
ct <- getCPUTime
(TOD sec _) <- getClockTime
return (createStdGen (sec * 12345 + ct + o))
This should address your concerns. I'm reluctant to change the
classic Hugs implementation at this stage. (It was a 'small' change
like that caused the getChar to start failing on some Win boxes).
The plan is to make only bug fixing releases of classic Hugs,
until the next generation Hugs (currently code named STG Hugs)
is stable enough to fully subsume classic Hugs. We are expecting
to release a "thrill seeks" release of STG Hugs at some point this
week. A more stable official release of STG Hugs is expected
Feb/March 2000.
So we will be making only bug fixing releases of classic Hugs
(as needed) putting to work into getting the new Hugs working better.
The new Hugs already has FFI (thanks to the hard work of Julian
Seward), for example. And a better random seed generator :-)
Andy Gill
--
Principal Project Scientist, Pacific Software Research Center
Department of Computer Science and Engineering
Oregon Graduate Institute of Science & Technology
phone +1 503 748 7451 http://www.cse.ogi.edu/~andy