0

我无法从 MT 获得不同的随机数,种子具有固定值(未按照测试人员的建议进行更改)。但是当我在一个程序中调用同一个函数两次时,它会显示不同的值,但即使我多次调用该程序,它也不会随机化值。我不明白我落后的地方。
请就此向我提出建议

下面是从我的 .asc 文件中粘贴的代码(与闪存媒体服务器相关)

主程序

  load(shuffle.asc);
  application.onAppStart=function()
  {
trace("Application Started");
  };

  application.onConnect=function(client,name)
   {

shuffledNumbers();
   }

随机播放.asc

var gen_random;


function  shuffledeck()
{
trace("shufledeck");

gen_random = new Array();

gen_random= new Array();

for(i=0;i<1000;i++)
{
gen_random[i]=genrand_int32();
}
trace("gen_random: "+gen_random); // HERE WE CAN SEE THE RANDOMLY GENERATED NUMBERS . THE PROBLEM: EVERYTIME I AM GETTING THE SAME RANDOM GENERATED VALUES.

}


    var N = 624;  
    var M  = 397;  
    var MATRIX_A  = 0x9908b0df;   /* constant vector a */  
    var UPPER_MASK  = 0x80000000; /* most significant w-r bits */  
    var LOWER_MASK  = 0x7fffffff; /* least significant r bits */  
    var mt =[]; /* the array for the state vector  */  
    var mti ;  
    var seed ;  
    var returnLength ;  
    var maxSize ;  
    var returnArray = new Array();




    /* initializes mt[N] with a seed */  
    function init_genrand($seed)
    {
        mt[0]= $seed & 0xffffffff;

        for (mti=1; mti<N; mti++) {
            mt[mti] = (1812433253 * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
            mt[mti] &= 0xffffffff;
            /* for >32 bit machines */


        }
    }

    /* initialize by an array with array-length */
    /* init_key is the array for initializing keys */
    /* key_length is its length */
    /* slight change for C++, 2004/2/26 */
    //    void init_by_array(unsigned long init_key[], int key_length)  

    /* generates a random number on [0,0xffffffff]-interval */  
    function genrand_int32()     
    {
        var y ;
        var mag01 =[0x0, MATRIX_A];
        /* mag01[x] = x * MATRIX_A  for x=0,1 */

        if (mti >= N)
        {
            /* generate N words at one time */
            var kk ;

            if (mti == N+1)   /* if init_genrand() has not been called, */
                init_genrand(15475454); /* a default initial seed is used */

            for (kk=0;kk<N-M;kk++) {
                y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
                mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
            }
            for (;kk<N-1;kk++) {
                y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
                mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
            }
            y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
            mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];

            mti = 0;
        }

        y = mt[mti++];

        /* Tempering */
        y ^= (y >> 11);
        y ^= (y << 7) & 0x9d2c5680;
        y ^= (y << 15) & 0xefc60000;
        y ^= (y >> 18);
    //  trace("y: "+y);
        return y;
    }

    /* generates a random number on [0,0x7fffffff]-interval */  
    function genrand_int31()     
    {
        return (genrand_int32()>>1);
    }

任何帮助都会得到帮助

PS:我试图通过重新设置 int_genrand() 函数并获取输出来说服他们,但它似乎损害了随机性的可预测性。

4

1 回答 1

0

如果您尝试洗牌,我发现此功能很有用:

function fisherYates (myArray) {
  var i = myArray.length;
   while ( i-- ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
  }
}

用法:

myArray = [1,2,3,4,5];
fisherYates(myArray);
于 2013-03-06T05:41:56.347 回答