I have to create a random sequence of number using MersenneTwister (I can't change algorithm). Different calls to the generation algorithm always gives the same results after the first three attempts. Here is the code:
var getRandomArray=function(length){
var casuals=[];
var mersenneTwister = new MersenneTwister(new Date().getTime()/2); //A SEED TEST
while(casuals.length < length){
var vmt= mersenneTwister.random();
var newVal= Math.round( vmt*100);
if(casuals.indexOf(newVal)===-1)
casuals.push(newVal);
}
//casuals.sort(function(a,b){return a-b;});//NUMERIC SORT
return casuals;
};
var inputs1= getRandomArray(6);
var inputs2= getRandomArray(6);
//inputs1 AND inputs2 ARE ALWAYS THE SAME!
I tried changing different kind of seed but nothing changes. I created here a fiddle.
Here the algorithm I downloaded.
Am I missing something?
It is because you are using new Date().getTime()
. The twister function finishes very quickly, to two successive calls to new Date().getTime()
give the same value, thus the same seed.
If you add a different number, e.g. such as this:
it works as expected.
The changes are:
$scope.genRandom=function(){
$scope.inputs1= getRandomArray(6, 1);
$scope.inputs2= getRandomArray(6, 2);
};
and:
var getRandomArray=function(length, add){
var casuals=[];
var mersenneTwister = new MersenneTwister(new Date().getTime()/2 + add);
...
}
i.e. addition of add
parameter.