Random, not so random?

Jesse Phillips jessekphillips+d at gmail.com
Sun Oct 23 10:50:07 PDT 2011


In this little code example I'm trying to use the same random number 
generator to produce a randomCover over an array multiple times.

I make 4 assertions, the first two pass, the last two fail. One of the 
benefits for providing a seed to a generator is that you can reproduce 
the behavior, but at the same time the generator can be used throughout 
the program to create random events. Instead, the events are the same.

This is a bug right?

import std.array;
import std.conv;
import std.random;

void main() {
    auto arr = [1,2,3,4];
    auto gen = Random(unpredictableSeed);

    assert(randomCover(arr,gen) != randomCover(arr,gen));

    auto result1 = randomCover(arr,gen);
    auto result2 = randomCover(arr,gen);
    assert(result1 != result2);

    auto arr1 = array(randomCover(arr,gen));
    auto arr2 = array(randomCover(arr,gen));
    assert(arr1 != arr2);

    auto str1 = to!string(randomCover(arr,gen));
    auto str2 = to!string(randomCover(arr,gen));
    assert(str1 != str2);
}


More information about the Digitalmars-d-learn mailing list