randomCover not so random?

dsimcha dsimcha at yahoo.com
Mon Apr 20 13:57:52 PDT 2009


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))) {
            result ~= elem;
        }
        counts[result]++;
    }
    foreach(k, v; counts) {
        writeln(k, "\t", v);
    }
}

Output:

2 3 1   0
3 2 1   0
1 3 2   0
3 1 2   0
1 2 3   4929
2 1 3   5071

Note that when I use randomShuffle instead of randomCover, I get a fairly
uniform distribution on the permutation space.



More information about the Digitalmars-d mailing list