Confusion about `Random`

Ali Çehreli acehreli at yahoo.com
Sat Dec 24 16:34:29 UTC 2022


On 12/24/22 08:18, jwatson-CO-edu wrote:
 > On Friday, 23 December 2022 at 07:25:23 UTC, Salih Dincer wrote:
 >> You can try using static this.
 >>
 >> ```d
 >> import std.random;
 >>
 >> static this() { } // can try using

static this() blocks: executed when a thread a starts (the program has 
at least one thread: the main thread); so you can put initializations here

~static this() blocks: counterparts of 'static this', executed once for 
each thread that is terminating

shared static this() blocks: executed once per program (executed by the 
main thread)

~shared static this() blocks executed once per program when the program 
is terminating

 >>     "rand" : () => new Atom(rand01)

That's the lambda (ananymous function) syntax.

The "rand" key of an associative array is being associated with the 
function after the ':' character. In the code above, the function 
creates a new Atom object. So, when the following code is executed:

   primitiveSymbols["rand"]

the same lambda would be returned but the execution of that lambda as 
the following

   primitiveSymbols["rand"]()

would return a new Atom which would make a call to the rand01() function 
and get a new random number from the same 'rnd' object.

Ali



More information about the Digitalmars-d-learn mailing list