randomCover not so random?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Apr 20 14:19:04 PDT 2009


dsimcha wrote:
> Maybe I'm doing something subtly wrong here, but I've checked all the obvious
> stuff.
> 
> import std.stdio, std.random, std.array;
> 
> import dstats.base : Perm;
> 
> void main() {
>     uint[int[]] counts;
>     immutable int[] foo = [1,2,3];
> 
>     // Make sure every permutation is represented in counts.
>     foreach(perm; Perm!int(foo.dup)) {
>         counts[perm.dup] = 0;
>     }
> 
>     foreach(i; 0..10_000) {
>         int[] result;
>         foreach(elem; randomCover(foo, Random(unpredictableSeed))) {

Here you reseed the random number generator every pass through the outer 
loop. Although unpredictableSeed is designed to be unpredictable, it is 
_not_ random.

You'd need to have each cover use the same random generator. Here's 
where a shortcoming in the design of randomCover becomes evident: 
randomCover stores a copy of its generator, which makes it difficult to 
support what you need. I will look into a fix.


Andrei



More information about the Digitalmars-d mailing list