Is continuously seeding a random number generator performance intensive?

Jeroen Bollen jbinero at gmail.com
Tue Jan 21 10:59:00 PST 2014


On Tuesday, 21 January 2014 at 17:51:44 UTC, monarch_dodra wrote:
> On Tuesday, 21 January 2014 at 17:13:39 UTC, Jeroen Bollen 
> wrote:
>> On Friday, 17 January 2014 at 19:00:29 UTC, Jeroen Bollen 
>> wrote:
>>> On Wednesday, 15 January 2014 at 21:00:57 UTC, Jeroen Bollen 
>>> wrote:
>>>> How do you correctly create a MersenneTwisterEngine with a 
>>>> ulong as seed?
>>>
>>> This question still isn't answered by the way.
>>
>> Come on, surely someone knows how to. I've already tried the 
>> obvious, but it doesn't work.
>>
>> MersenneTwisterEngine!(ulong, 32, 624, 397, 31, 0x9908b0df, 
>> 11, 7, 0x9d2c5680, 15, 0xefc60000, 18) rndEngine;
>> rndEngine = MersenneTwisterEngine(seed);
>>
>> "shift by 32 is outside the range 0..31"
>> "static assert  (false && 4022730752LU <= 0LU) is false"
>> "instantiated from here: MersenneTwisterEngine!(ulong, 32, 
>> 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 
>> 4022730752u, 18)"
>
> Is that your actual code? "MersenneTwisterEngine(seed)" is not 
> valid code, you have to provide the template arguments.
>
> In any case, that's a bug. Please file a report for it, and 
> I'll give it a full fix.
>
> For a local fix, you can replace the incriminating line 
> (random.d:550):
>     /// Largest generated value.
>     enum UIntType max =
>         w == UIntType.sizeof * 8 ? UIntType.max : (1u << w) - 1;
>
> By:
>     /// Largest generated value.
>     enum UIntType max =
>         UIntType.max >> (UIntType.sizeof * 8 - w);
>
> I think that's all that's required.
>
> PS: If you want to generate longs, you also need to set the "w" 
> variable (wordSize) from 32 to 64, or you'll still just be 
> generating 32 bits worth of randomness.
>
> I also recommend you use an alias (and alignment), such as:
>
>     alias Mt19937UL =
>         MersenneTwisterEngine!(ulong, 64, 624, 397, 31,
>                                0x9908b0df, 11, 7,
>                                0x9d2c5680, 15,
>                                0xefc60000, 18);
>     auto rndEngine = Mt19937UL(seed);
That is my actual code.


More information about the Digitalmars-d-learn mailing list