Random Numbers

Ralph Main rmain1972 at live.com
Sat Aug 4 10:47:52 PDT 2012


On Saturday, 4 August 2012 at 17:45:19 UTC, Ralph Main wrote:
> On Saturday, 4 August 2012 at 17:02:19 UTC, bearophile wrote:
>> Ralph Main:
>>
>>> The module std.random documentation doesn't work as per the 
>>> examples.
>>> The example shows getting a random number by the following 
>>> code does not work:
>>>
>>> <code>
>>>   // Generate a uniformly-distributed integer in the range 
>>> [0, 14]
>>>   auto i = uniform(0, 15);
>>>   // Generate a uniformly-distributed real in the range [0, 
>>> 100)
>>>   // using a specific random generator
>>>   Random gen;
>>>   auto r = uniform(0.0L, 100.0L, gen);
>>> </code>
>>>
>>> <code>
>>>   // Gets a random number
>>>   int get_random() {
>>>       auto rng = Random(unpredictableSeed);
>>>       auto rn = uniform(0, m_files.length, rng);
>>>       return rn;
>>>   }
>>> </code>
>>
>> This code works, but it's a bad idea to create a new generator 
>> inside getRandom():
>
> get_random is inside of a class.  Because of scope problems, I 
> don't think I can put it anywhere else.  It would be better if 
> you could create rng as
>
> int rng;
> rng = Random(unpredictableSeed);
>
> then you would be able to put it elsewhere.
>
> Ralph

I did take out the new keyword. So I guess the garbage collector 
will be happier. ;)





More information about the Digitalmars-d-learn mailing list