简体   繁体   English

MersenneTwister重复数字

[英]MersenneTwister repeated numbers

I have to create a random sequence of number using MersenneTwister (I can't change algorithm). 我必须使用MersenneTwister创建一个随机的数字序列(我无法更改算法)。 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() . 这是因为您正在使用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. 扭曲函数的完成非常快,对new Date().getTime()两次连续调用将赋予相同的值,因此具有相同的种子。

If you add a different number, eg 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);
...
}

ie addition of add parameter. add参数的add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM