Random, not so random?

Kagamin spam at here.lot
Mon Oct 24 08:06:19 PDT 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.


More information about the Digitalmars-d-learn mailing list