MATLAB: How to match Matlab and original Mersenne Twister RN streams

mersenne twisterrng

Using Matlab R2017_a on a Mac OS X 10.11.6 (El Capitan) I get the following output:
>> format long
>> rng(1, 'twister')
>> rand(10,1)
ans =
0.417022004702574
0.720324493442158
0.000114374817345
0.302332572631840
0.146755890817113
0.092338594768798
0.186260211377671
0.345560727043048
0.396767474230670
0.538816734003357
While, using the original MT code (<http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c>) using Eclipse version Mars.1 Release (4.5.1)
int main(void)
{
int i;
init_genrand(1);
printf("\n10 outputs of genrand_real2()\n");
for (i=0; i<10; i++) {
printf("%16.15f\n ", genrand_real1());
}
return 0;
}
I get:
10 outputs of genrand_real2()
0.417021998437122
0.997184808133170
0.720324489288032
0.932557361200452
0.000114381080493
0.128124447772279
0.302332567749545
0.999040515394881
0.146755892550573
0.236088976264000
Questions:
1) It seems that Matlab is taking only the odd values of the stream produced with the original code. Why ?
2) It seems that there is a mismatch at the level of the sixth digit. Why ?
Thanks for helping.

Best Answer

Hi,
it is because Matlab uses the genrand_res53()-function. It uses two rand-calls, that is why every second value is not used. The precision is then exact.