Confusion about `Random`
Salih Dincer
salihdb at hotmail.com
Fri Dec 23 07:25:23 UTC 2022
On Thursday, 22 December 2022 at 16:23:16 UTC, jwatson-CO-edu
wrote:
> What is the reason for this? Has the compiler optimized away
> the `uniform` call to a single double number?
> What is the main difference between Program 1 and Program 2?
> Both seem to:
> * Have a global RNG `rnd`
> * Seed RNG after `main` starts.
> * Generates a random number on [1,0) from a function.
>
> So I would expect both programs to behave the same...
I made your code runnable based on the information you provided
us. There seems to be no problem if you try. You can try using
static this.
```d
import std.random;
static this() { } // can try using
Mt19937 rnd;
void init_random() {
rnd = Random(unpredictableSeed);
}
double rand01() {
return uniform(0, 1.0, rnd);
}
void main()
{
init_random();
struct Atom { double num; }
alias atom = Atom* function();
atom[string] primitiveSymbols = [
"rand" : () => new Atom(rand01)
];
import std.stdio;
writeln(*primitiveSymbols["rand"]()); // Atom(0.630001)
}
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list