1 Reply Latest reply on May 11, 2011 8:07 AM by Ned Murphy

    Random No. store in Array

    JaxNa Level 1

      Hi Guys,

       

      I want random no between 1 to 15 store in my array.

       

      but that time not repeat the any no.

       

      for e.g myArray(4,6,8,9,2,3,5,1,7,10,13,15,12,11)

       

      can you check my code.

       

      ******************************************************

      var randomNo:Number;

      var holdArray:Array = new Array();

      for (var i:Number = 0; i < 15; i++)

      {

      randomNo = Math.round(Math.random() * (15 - 1)) + 1;

      holdArray.push(randomNo);

      }

      ******************************************************

       

       

       

      Thanks,

      JaxNa

        • 1. Re: Random No. store in Array
          Ned Murphy Adobe Community Professional & MVP

          Your code will repeat values randomly.  Try the following instead...

           

          var holdArray:Array = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

           

          function shuffle(a) {
              var p;
              var t;
              var ivar;
              for (ivar = a.length-1; ivar>=0; ivar--) {
                  p=Math.floor((ivar+1)*Math.random());
                  t = a[ivar];
                  a[ivar] = a[p];
                  a[p] = t;
              }
          }

           

          shuffle(holdArray);

           

          trace(holdArray);