Question

I am trying to implement Mersenne Twister algorithm in PHP. I have taken the Javascript implementation from http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/JAVASCRIPT/java-script.html

I have almost made a line to line conversion from the above js code. But, the random number generated for a given seed is different in PHP.

Are there any specific steps to be followed when implementing in PHP (Choosing datatypes etc) ?

Code snippet as below

/************* PHP ***************************/


<?php

require_once "mersenne_twister.php";
use mersenne_twister\twister;

$range = array(1,2,3,5,6,7,8,9,12,13,15);
$twister1 = new twister(1347299472.0059);
$t1 =  $twister1->int32()* (1.0 / 4294967296.0) ;
$foo = $t1 * count($range);
echo $foo;


/************* Javascript ***************************/

var seed = 1347299472.0059;
var range = [1,2,3,5,6,7,8,9,12,13,15];// the numbers to chose from
var foo = Math.floor(genrand_float(range ) * range .length);
document.write(foo);

$twister1->int32() * (1.0 / 4294967296.0) and genrand_float(range) gives different output.

Thanks,

Was it helpful?

Solution

The mt_rand() function already implements the Mersenne Twister algorithm, there's no need to write your own version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top