Random Numbers

Ralph Main rmain1972 at live.com
Sat Aug 4 09:40:48 PDT 2012


Gotchas!

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 = new Random(unpredictableSeed);
         auto rn = uniform(0, m_files.length, rng);
         return rn;
     }
</code>

The new keyword was not in the example, and the original example 
code would not work.  When looking at the source code of the std 
libraries, a struct can contain a constructor, so therefore it is 
similar to a class; and on a whim I tried the new keyword.  So I 
thought I would pass this information along.  I looked at other 
posts in the forum, but didn't see anyone using the new keyword.  
Is this a bug, or a change to the D language implementation?


More information about the Digitalmars-d-learn mailing list