5 Replies Latest reply on Nov 19, 2006 8:40 PM by kglad

    Random - no repeat...

    DIY_Lobotomy Level 1
      I need to get a set of random numbers into an array, but I can't have any numbers repeat. So, as a practical example I will need, say, 10 numbers that all fall between, say, 1 and 50, but I need ALL 10 numbers to be different. Order is not important (lowest to highest, highest to lowest, mixed-up, whatever - as long as there's no duplicates). Does that make sense?...

      I currently can get the numbers into the array, but I can't figure out what I need to change to get them to not duplicate. Here's what I currently have:

        • 1. Re: Random - no repeat...
          McWeeney
          How about putting all the numbers 1 to 50 in an array, generating a random number up to the lenght of the array and using splice to remove the number in that random position and push it into the array you want. put the splice and push within a for loop. SInce you are removing numbers from the original array you will never get the same one twice.

          HTH
          • 2. Re: Random - no repeat...
            DIY_Lobotomy Level 1
            Okay - that worked well. I just need to know how to convert an item in an array to a number. Because, what I want to use these random numbers for are to plug into "gotoAndPlay(n)". So, I need them to be numbers and not strings. Here's what I have so far that works to get my properly randomized array. Please advise - thanks!:
            • 3. Re: Random - no repeat...
              Designforce
              I use this for randimized functions with no repeats:
              quest_XX is a function which is called from the array. Each function in this case is a quiz item. Once user clicks button, the function for right or wrong is called and then the randomizer function is called.
              Hope this helps....
              arr = new Array();
              arr[0] = quest_1;
              arr[1] = quest_2;
              arr[2] = quest_3;
              arr[3] = quest_4;
              arr[4] = quest_5;
              arr[5] = quest_6;
              arr[6] = quest_7;
              arr[7] = quest_8;
              arr[8] = quest_9;
              arr[9] = quest_10;
              arr[10] = quest_11;
              ////////////////////////////////////////////nine 9
              arr[11] = quest_12;
              arr[12] = quest_13;
              arr[13] = quest_14;
              arr[14] = quest_15;
              arr[15] = quest_16;
              arr[16] = quest_17;
              arr[17] = quest_18;
              arr[18] = quest_19;
              arr[19] = quest_20;
              arr[20] = quest_21;
              //////////////////////////////////////////eight 8
              arr[21] = quest_22;
              arr[22] = quest_23;
              arr[23] = quest_24;
              arr[24] = quest_25;
              arr[25] = quest_26;
              arr[26] = quest_27;
              arr[27] = quest_28;
              arr[28] = quest_29;
              arr[29] = quest_30;
              arr[30] = quest_31;
              /////////////////////////////////////////////seven 7
              arr[31] = quest_32;
              arr[32] = quest_33;
              arr[33] = quest_34;
              arr[34] = quest_35;
              arr[35] = quest_36;
              arr[36] = quest_37;
              arr[37] = quest_38;
              arr[38] = quest_39;
              //////--------------randomizer uses pop function - call and remove last item in array--------///////////////////////
              function randomizer() {
              if (myscore == 39) {
              end_line();
              }
              // shuffle array by switching elements places
              for (i=0; i<39; i++) {
              popped = arr.pop();
              place = Math.floor(Math.random()*arr.length);
              arr.splice(place, 0, popped);
              }
              // since array is shuffled, every taken element is random
              var b = arr.pop();
              b();
              }
              /////////////////////////////wrong - right - answer buttons
              function wrong() {
              missed.missed += 1;
              myscore += 1;
              randomizer();
              }
              function right() {
              correct.correct += 1;
              myscore += 1;
              randomizer();
              }
              • 4. Re: Random - no repeat...
                german01 Level 1
                McWeeney:

                Can you please share the script you used to randomly (not repeatedly) play your video clips from an array?
                I'm dealing with similar issue, but I can't make my script work for me:


                thanks

                German
                • 5. Re: Random - no repeat...
                  kglad Adobe Community Professional & MVP
                  apply the shuffle() method to any array. the array items will then be shuffled so accessing them from element zero to the last element generates a sequence of array elements chosen randomly without duplicates: