Summary

random seed should be more random

Metadata

Description

The default RNG is seeded with time(NULL) which only changes every 1 second. See CHICKEN_initialize in runtime.c. Actually it only changes every 2 seconds, as C_randomize expects a C_word and the bottom bit is therefore destroyed. Actually, on most platforms it may take up to a minute to change values, as rand() is crap. Example:

 
 Instead it should be seeded with pid XOR seconds XOR milliseconds.  This is totally portable and doesn't involve replacing rand() with random() or anything else disruptive, just seeding with more random bits.
 
 I will put together a patch to do this, it is a trivial fix.

Changes and comments

[2011-12-30 00:28:16 UTC] zbigniew wrote:

Even worse, (randomize) only generates a new result once every 1,000 seconds, as (current-seconds) is divided by 1000. I will aim to fix this as well.

[2011-12-30 05:42:04 UTC] zbigniew changed priority from major to not urgent at all

[2011-12-30 05:42:04 UTC] zbigniew changed status from new to assigned

[2011-12-30 05:42:04 UTC] zbigniew changed type from defect to enhancement

[2011-12-30 05:42:04 UTC] zbigniew removed owner zbigniew

[2011-12-30 05:42:04 UTC] zbigniew removed milestone 4.8.0

[2011-12-30 05:42:04 UTC] zbigniew wrote:

I guess it is not trivial, the core would not support it without some disruptive changes, i.e. risking compilation failures on unusual platforms.

1) gettimeofday is not available in core on non-UNIX platforms, even though it is available in both mingw and cygwin themselves. Maybe that is historical. 2) getpid is available on unix and windows but perhaps not elsewhere?

In short, there's no easy way to get the process pid or the usec since epoch at initialization time from runtime.c.

With regard to Peter's hash table security patch, which was the impetus for this ticket -- as it requires a random fixnum when SRFI-69 is loaded --, an ugly workaround should be to use the randomization key in make-hash-table, as in

 (use random-bsd)
 (make-hash-table eq? hash size randomization: (random-fixnum 536870912))



 
 I'll leave this open, but at the lowest possible priority.

[2016-08-25 21:05:24 UTC] sjamaan set difficulty to hard

[2018-04-27 21:56:16 UTC] felix set resolution to fixed

[2018-04-27 21:56:16 UTC] felix wrote:

Fixed in CHICKEN 5 with the new RNG.

[2018-04-27 21:56:16 UTC] felix changed status from assigned to closed