Thread overview
Random, not so random?
Oct 23, 2011
Jesse Phillips
Oct 23, 2011
Jesse Phillips
Oct 24, 2011
Kagamin
Oct 24, 2011
Jesse Phillips
Oct 24, 2011
bearophile
Oct 25, 2011
Kagamin
October 23, 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);
}
October 23, 2011
I'm thinking something like this is appropriate, though I removed the save
() feature.

https://gist.github.com/1307739
October 24, 2011
Jesse Phillips Wrote:

> 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);

these structs are not equal because they allocated different arrays.

>     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);
> }

these fail because Random is a struct an its state is precisely replicated on copy, so two calls to randomCover accept gen in the same state, so their outputs are identical. All arr1, arr2, str1 and str2 are equivalent.
October 24, 2011
Kagamin Wrote:

> these fail because Random is a struct an its state is precisely replicated on copy, so two calls to randomCover accept gen in the same state, so their outputs are identical. All arr1, arr2, str1 and str2 are equivalent.

Yes, I found that. But the question remains, is it a bug. As I said reuse of a generator is good to allow random behavior throughout the program, but reproducible behavior throughout each run.

RandomCover is a very useful for selecting all options of a range. But you must create a new generator every-time to get random behavior from sequential calls.
October 24, 2011
Jesse Phillips:

> Kagamin Wrote:
> 
> > these fail because Random is a struct an its state is precisely replicated on copy, so two calls to randomCover accept gen in the same state, so their outputs are identical. All arr1, arr2, str1 and str2 are equivalent.
> 
> Yes, I found that. But the question remains, is it a bug. As I said reuse of a generator is good to allow random behavior throughout the program, but reproducible behavior throughout each run.

Time ago I have added this enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=6593

Bye,
bearophile
October 25, 2011
Jesse Phillips Wrote:

> RandomCover is a very useful for selecting all options of a range. But you must create a new generator every-time to get random behavior from sequential calls.

Also it's probably a big structure. Check its size.