0

I am unable to get different random numbers from the MT,having seed with a fixed value(not changing as suggested by the testers). But when i call the same function twice in a program, it shows different values, but it don't randomize values even if i call the program multiple times.I don't understand where i am lagging behind.
Please suggest me on this

Below is the code pasted from mine .asc file(related to flash media server)

main.asc

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

  application.onConnect=function(client,name)
   {

shuffledNumbers();
   }

shuffle.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);
    }

Any help will be appriciated

PS: I have tried to convince them, by reseeding the int_genrand() function, and getting the output but, it compromises the predictability of randomness itseems.

user1647017
  • 283
  • 1
  • 2
  • 14
  • 1
    Wath's wrong with builtin Math.random()? – Smolniy Mar 05 '13 at 14:22
  • 1
    @Smolniy Its deck of cards, which I need to shuffle. There we cant use, math.random(), so I am using MT algorithm to get RNG values – user1647017 Mar 06 '13 at 05:17
  • You say "reseeding the int_genrand() would compromises the predictability of randomness". To me, whether you want predictable random numbers (the result you have right now) or not (what you seems to want) – duTr Mar 28 '13 at 06:23

1 Answers1

0

If you are attempting to shuffle a deck of cards, I have found this function to be useful:

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;
  }
}

usage:

myArray = [1,2,3,4,5];
fisherYates(myArray);
danjp
  • 717
  • 1
  • 5
  • 20
  • This is the same i am gonna use in later stage, but, at the place of `Math.random()` function , i need to use the function i have mentioned above i.e `shuffle()` function. – user1647017 Mar 06 '13 at 07:42
  • Hello @danjp, @Smolniy, I think I have got the output, just by keeping the line `init_genrand(15475454);` outside the function(making it global) Please let me know, is mine approach correct, Thank You – user1647017 Mar 06 '13 at 08:06
  • 2
    I'm not sure I understand the problem correctly. (in the question) In main.asc you call `shuffledNumbers()`, but there is no function with name `shuffledNumbers` in shuffle.asc. But usually when you use random generator with seed function, you need to call init seed function with current date as argument before use generator. Maybe in your case calling `init_genrand((new Date()).getTime());` will resolve the problem. – Smolniy Mar 06 '13 at 12:32