www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Random, not so random?

reply Jesse Phillips <jessekphillips+d gmail.com> writes:
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);
}
Oct 23 2011
next sibling parent Jesse Phillips <jessekphillips+d gmail.com> writes:
I'm thinking something like this is appropriate, though I removed the save
() feature.

https://gist.github.com/1307739
Oct 23 2011
prev sibling parent reply Kagamin <spam here.lot> writes:
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.
Oct 24 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Oct 24 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Oct 24 2011
prev sibling parent Kagamin <spam here.lot> writes:
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.
Oct 25 2011