The C++ standard library does contain a set of random distribution classes, among other things a Mersenne Twister engine.
What I'd like to do is to generate the same random number distribution, using the same seed for C++ and Mathematica.
For instance, generate 10 random numbers using the mersenne twister engine (C++):
std::vector<size_t> a1( 10 );
std::mt19937 gen(42);
std::generate(a1.begin(), a1.end(), gen);
/* {1608637542, 3421126067, 4083286876, 787846414, 3143890026, 3348747335,
2571218620, 2563451924, 670094950, 1914837113} */
and now I generate 10 random integers in Mathematica using the MersenneTwister engine:
BlockRandom[SeedRandom[42, Method -> "MersenneTwister"]
RandomInteger[{0, 2^32 - 1}, 10]]
(* {3012359023, 1649004928, 188383571, 1935467488, 3494372723, 668236121,
1292572136, 98984411, 2487091843, 3121826951} *)
Apparently not the same. The mersenne twister in the C++ standard library uses the following default settings to generate the distribution:
The question is now: How much influence do I have on Mathematica's random number generator, so that I can overwrite some specific settings to generate the same set of numbers for a given seed?
EDIT: MathLink implementation MT19937Range:
The .tm file:
:Begin:
:Function: mersenneRange
:Pattern: MT19937Range[seed_, len_]
:Arguments: { seed, len }
:ArgumentTypes: { Integer, LongInteger }
:ReturnType: Manual
:End:
:Evaluate: MT19937Range::usage = ""
The .cpp file:
#include <random>
using namespace std;
void mersenneRange( int seed, long length )
{
mt19937 gen(seed);
MLNewPacket(stdlink);
MLPutFunction(stdlink, "List", length);
for(long x = 1; x <= length; x++)
MLPutLongInteger(stdlink, gen());
}
MLPutFunction expects as a third argument a int. So implicit conversion here with loss of precision. Is there a better alternative?
The Mathematica side:
link = Install["wherever the binary is...", LinkMode -> Launch]
MT19937Range[42, 10]
(* {1608637542, 3421126067, 4083286876, 787846414, 3143890026, 3348747335, 2571218620,
2563451924, 670094950, 1914837113} *)
Uninstall[link]