3 Replies Latest reply on Jun 17, 2013 8:12 AM by resdesign

    How to display 20 random symbols from pool of 50?

    desziner Level 1

      Hi,

      I have a project that plays a random symbol when a button is clicked, however I want a random symbol to be shown each time the button is pressed for 20 times.

       

      How do I limit the display to just show 20 random symbols from a pool of 50?

       

      This is the code I am using on the button that displays a random symbol each time clicked:

       

      // RADOMIZE

                var myVar = Math.floor((Math.random()*4)+1);

       

      // create symbol into container

                var container = sym.$('container');

                var myAni = sym.createChildSymbol("Symbol_" + myVar, container);

                myAni.getSymbolElement().css({

                'position': 'absolute',

                'left': 0,

                'top': 0,

       

       

      });

       

       

      Thanks

        • 1. Re: How to display 20 random symbols from pool of 50?
          resdesign Adobe Community Professional & MVP

          You need to shuffle the array and then randomize the 20 elements".

           

          Here is a example showing you how to do with 4 elements:

           

          var originalArray=['one','two','three','four','five','six','seven','eight','nine','ten'];

           

          // shuffle the original array

          originalArray.sort(function() {return 0.5 - Math.random()});

          // then  limit the array to the 4 first elements

          originalArray.length =4;

          // the alert is to show that you have 4 elements and what they are - added 5 to show element 5 does not exist.

          alert(originalArray[0] + ' ' +originalArray[1] + ' ' +originalArray[2] + ' ' +originalArray[3] + ' ' +originalArray[4]);

          // randomize the elements in the new array

          var myVar = Math.floor((Math.random()*3)+1);

          sym.$('Text').click(function(){

                    // myVar++;

               // shows one of the 4 numbers

                    sym.$('Text').html(originalArray[myVar]);

          });

          • 2. Re: How to display 20 random symbols from pool of 50?
            desziner Level 1

            Thanks for your response.

            I am looking to display only one symbol at a time and reduced the alert to just show one output:

             

            alert(originalArray[0] );

             

            this shows one random symbol each time, but sometimes when clicking the button it will repeats the number previous.

             

            How do I display a unique number each time the button is clicked so it doesn't show again?

            • 3. Re: How to display 20 random symbols from pool of 50?
              resdesign Adobe Community Professional & MVP

              Change the code to this:

               

              var originalArray=['one','two','three','four','five','six','seven','eight','nine','ten'];

               

               

              originalArray.sort(function() {return 0.5 - Math.random()});

              originalArray.length =4;

               

               

              i=0;

              sym.$('Text').click(function(){

               

               

                        // shows one of the 4 elements on each click

                        sym.$('Text').html(originalArray[i]);

                                  i++;

              });

              1 person found this helpful