digitalmars.D.learn - Random, not so random?
- Jesse Phillips (24/24) Oct 23 2011 In this little code example I'm trying to use the same random number
- Jesse Phillips (3/3) Oct 23 2011 I'm thinking something like this is appropriate, though I removed the sa...
- Kagamin (3/20) Oct 24 2011 these fail because Random is a struct an its state is precisely replicat...
- Jesse Phillips (3/4) Oct 24 2011 Yes, I found that. But the question remains, is it a bug. As I said reus...
- bearophile (5/10) Oct 24 2011 Time ago I have added this enhancement request:
- Kagamin (2/3) Oct 25 2011 Also it's probably a big structure. Check its size.
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
I'm thinking something like this is appropriate, though I removed the save () feature. https://gist.github.com/1307739
Oct 23 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.
Oct 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.
Oct 24 2011
Jesse Phillips:Kagamin Wrote:Time ago I have added this enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=6593 Bye, bearophilethese 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.
Oct 24 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.
Oct 25 2011