www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Example for Mir-Random fails

reply Brad <bjazmoore outlook.com> writes:
I just do not know enough about the D libraries to figure out 
what is wrong.  I know it is a case of type mismatch.  The 
example appears on the Mir-Random page as listed under DUB:
https://code.dlang.org/packages/mir-random

The code looks like this:

```d
void main()
{
     import mir.random;
     import mir.random.variable: normalVar;
     import mir.random.algorithm: randomSlice;

     // Engines are allocated on stack or global
     auto rng = Random(unpredictableSeed);
     auto sample = rng.randomSlice(normalVar, 10);

     import std.stdio;
     sample[rng.randIndex($)].writeln;
}
```

This is the error I am seeing (I slapped the sample code into 
another program that flips a coin... It compiles file without the 
sample code):

```d
source\flipcoin.d(39,20): Error: constructor 
`std.random.MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 
31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
4022730752u, 18LU, 1812433253u).MersenneTwisterEngine.this(uint 
value)` is not callable using argument types `(ulong)`
source\flipcoin.d(39,20):        cannot pass argument 
`unpredictableSeed()` of type `ulong` to parameter `uint value`
```

Obviously it is a type mismatch - I have tried using to!uint to 
convert the result from unpredictableSeed to a type that will 
match - but that just causes more errors.

Thank you in advance.
Apr 03
next sibling parent reply Preetpal <preetpal.sohal gmail.com> writes:
On Saturday, 3 April 2021 at 19:02:34 UTC, Brad wrote:
 Obviously it is a type mismatch - I have tried using to!uint to 
 convert the result from unpredictableSeed to a type that will 
 match - but that just causes more errors.

 Thank you in advance.
I was able to compile the sample without any issue and it was able to run. Can you give more information about your environment and possibly create a minimal example that re-creates the issue like in a Github repository?
Apr 03
parent Brad <bjazmoore outlook.com> writes:
On Saturday, 3 April 2021 at 20:55:40 UTC, Preetpal wrote:
 On Saturday, 3 April 2021 at 19:02:34 UTC, Brad wrote:
 Obviously it is a type mismatch - I have tried using to!uint 
 to convert the result from unpredictableSeed to a type that 
 will match - but that just causes more errors.

 Thank you in advance.
I was able to compile the sample without any issue and it was able to run. Can you give more information about your environment and possibly create a minimal example that re-creates the issue like in a Github repository?
I figured out that I had not purged all the Phobos random imports from my code and the issue was related to the two libraries not playing well together. It is working now. Interestingly though - the example code will not compile in the D Playground. I am not going to worry about that. It is working for me now. Thanks
Apr 03
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Saturday, 3 April 2021 at 19:02:34 UTC, Brad wrote:
 I just do not know enough about the D libraries to figure out 
 what is wrong.  I know it is a case of type mismatch.  The 
 example appears on the Mir-Random page as listed under DUB:
 https://code.dlang.org/packages/mir-random

 [...]
I think you are seeing conflicts between Phobos and Mir: they both provide unpredictableSeed and Random. If you want to use the ones from Mir, be sure to not import std or std.random, or use fully qualified names. — Bastiaan.
Apr 03
parent Brad <bjazmoore outlook.com> writes:
On Saturday, 3 April 2021 at 22:04:33 UTC, Bastiaan Veelo wrote:
 On Saturday, 3 April 2021 at 19:02:34 UTC, Brad wrote:
 I just do not know enough about the D libraries to figure out 
 what is wrong.  I know it is a case of type mismatch.  The 
 example appears on the Mir-Random page as listed under DUB:
 https://code.dlang.org/packages/mir-random

 [...]
I think you are seeing conflicts between Phobos and Mir: they both provide unpredictableSeed and Random. If you want to use the ones from Mir, be sure to not import std or std.random, or use fully qualified names. — Bastiaan.
You nailed it. I just now figured that out. It never ceases to amaze me how much time can my lost to a simple mistake like that. Thank you.
Apr 03