libmir / mir-random

Advanced Random Number Generators
http://mir-random.libmir.org/
32 stars 15 forks source link

opCall template issues when using NormalVariable #114

Closed SaurabhTotey closed 5 years ago

SaurabhTotey commented 5 years ago

Hello, I was attempting to use normalVar when I ran into issues with the code not compiling.

template mir.random.variable.NormalVariable!double.NormalVariable.opCall cannot deduce function from argument types !()(MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 4022730752u, 18LU, 1812433253u)*), candidates are:
../../../.dub/packages/mir-random-2.1.5/mir-random/source/mir/random/variable.d(912,7):        mir.random.variable.NormalVariable!double.NormalVariable.opCall(G)(ref scope G gen) if (isSaturatedRandomEngine!G)
../../../.dub/packages/mir-random-2.1.5/mir-random/source/mir/random/variable.d(941,7):        mir.random.variable.NormalVariable!double.NormalVariable.opCall(G)(scope G* gen) if (isSaturatedRandomEngine!G)
/usr/bin/ldc2 failed with exit code 1.

These issues came when I tried using the following code.

import std.random;
import std.stdio;
import mir.random.variable;
Random gen = Random(unpredictableSeed);
double x = normalVar!double()(&gen);
writeln(x);

I referred to the documentation to try and get the code working, but I was unable to understand the examples using the undefined rne and threadLocalPtr. Furthermore, when I tried running the examples on the documentation, they seemed to fail. In short, I was wondering what the correct way to get a random double from the NormalVariable struct was.

9il commented 5 years ago

You are trying to use std.random with mir. std.random has unsafe api, so mir requires its own engines to be used. Use mir.random instead of std.random