generating random numbers

Ali Çehreli acehreli at yahoo.com
Mon Aug 10 06:10:58 UTC 2020


On 8/9/20 10:51 PM, Andy Balba wrote:
> generating random numbers using 
> https://dlang.org/library/std/random/uniform01.html
> 
> I find the example given in this section totally incomprehensible
> ... Can any help me answer two simple questions:
> How to generate a random floating number in range [0,1) ?
> How to set a seed value, prior to generating random values ?

I think feqrel() is the confusing and unnecessary part there. The 
following is all you need:

import std.stdio;
import std.random;

void main() {
   auto rnd = MinstdRand0(42);  // <-- Seed
   foreach (i; 0 .. 10) {
     writeln(rnd.uniform01());
   }
}

feqrel, defined at

   https://dlang.org/phobos/std_math.html#.feqrel

is used to prove that the first two floating point values generated are 
equal to 0.000328707 and 0.524587. (Check out the return value of 
feqrel, which is used in assert statements there to prove that the 
numbers are "equal" to those.)

Ali


More information about the Digitalmars-d-learn mailing list