9 Replies Latest reply on Jul 13, 2006 5:54 PM by Rothrock

    Generating random numbers that don't repeat

    sampurtill
      Hey there,

      Not sure if this has already been answered, because I think I saw it a really long time ago.

      I'll make it very simple. I have an array and I want to, based on the arrays length, make a new array and randomize the order that all the numbers in the new array will be in.

      So lets say this is my array

      var test_arr:Array = new Array();
      test_arr = [40, 80, 120, 160, 200, 240]

      And after I send it through the, say jumbleArray() function, it comes back with a new array, looking like this

      new_arr = [160, 240, 80, 200, 40, 120]

      Any help here? Thanks!!!
        • 1. Re: Generating random numbers that don't repeat
          sampurtill Level 1
          Whoops! Nevermind... I should have searched before I posted.

          Does anyone know a way that I could make this so that it is a function I can use in any of my flash documents? Like, is the some actionscript that is in a special directory that I could insert it in? Let me know, that'd be great. Thanks

          Array.prototype.shuffle = function() {
          var myArray:Array = new Array();
          for (i=0; i<this.length; i++) {
          var control:Boolean = true;
          while (control) {
          j = int(random(this.length));
          if (myArray[j] == undefined) {
          myArray[j] = this ;
          control = false;
          }
          }
          }
          return myArray;
          };
          • 2. Generating random numbers that don't repeat
            Craig Grummitt Level 3
            you can place this code in a class and call it up when you need it.
            • 3. Re: Generating random numbers that don't repeat
              sampurtill Level 1
              Craig Grummitt! Dude, you're a genius. Your code makes a lot more sense; I can actually read it and totally understand what's going on. On the other shuffle function, it didn't make complete sense.

              I'll put it in a class file, I was just wondering if I could insert the actionscript in some file that Flash always has, so I don't have to import classes. Something like the Array class is built in and always there, and I was wondering if I could put the code into that native class.

              Thanks,

              Sam
              • 4. Re: Generating random numbers that don't repeat
                Level 7
                Sam,

                > I'll put it in a class file, I was just wondering if I could
                > insert the actionscript in some file that Flash always has,
                > so I don't have to import classes.

                You don't have to import classes in ActionScript 2.0. By adding the
                path to this class file to your global classpaths setting, it will always
                just "be there." In AS2, import isn't required ... it's just a convenience
                that allows you to omit package names.

                http://www.communitymx.com/content/article.cfm?cid=197DE

                > Something like the Array class is built in and always there,
                > and I was wondering if I could put the code into that native
                > class.

                I wonder if you're thinking about prototyping. If so, that's an AS1
                approach, and only affects the prototyped classes in the current SWF
                (meaning, you have to type in the code that prototypes the desired class
                every time, or #include it).


                David
                stiller (at) quip (dot) net
                Dev essays: http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: Generating random numbers that don't repeat
                  sampurtill Level 1
                  Ok, I totally understand what you're saying David. I was just wondering if there's a way I could use it as if it were part of the array class. So I could just simply do this

                  var test_arr:Array = new Array(1, 2, 5, 6, 7, 8);
                  var shuffled_arr:Array = test_arr.shuffle();

                  And those would be the only two lines of code in my actions frame. I don't want to have to do

                  var shuffled_arr:Array = new com.sdpurtill.ShuffleArray(test_arr);

                  Understand? Thanks,

                  Sam
                  • 6. Re: Generating random numbers that don't repeat
                    Level 7
                    Sam,

                    > I was just wondering if there's a way I could use it as
                    > if it were part of the array class. So I could just simply
                    > do this
                    >
                    > var test_arr:Array = new Array(1, 2, 5, 6, 7, 8);
                    > var shuffled_arr:Array = test_arr.shuffle();

                    I'm with ya. Or even just ...

                    var test_arr:Array = new Array(1, 2, 5, 6, 7, 8);
                    test_arr.shuffle();

                    And you *can* do that with the Object.prototype property, inherited by
                    all objects, of course. But you'd have to type (or #include) the
                    ActionScript that defines the prototyped method every time.


                    David
                    stiller (at) quip (dot) net
                    Dev essays: http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: Generating random numbers that don't repeat
                      Rothrock Level 5
                      Sorry I don't have anyway for it to magically become part of every file, but the following will be even less processor heavy than the splice technique. We've tested it a lot and this is the fastest way to shuffle an array. So if and when you find the way that you are going to implement this you might want to base whatever you do off of this algorithm. :)
                      • 8. Re: Generating random numbers that don't repeat
                        sampurtill Level 1
                        Thanks David and Rothrock.

                        One more question, to Rothrock... Did you forget a line of code?

                        //
                        return this;
                        //

                        Array.prototype.shuffle = function() {
                        for (var i = this.length-1; i>0; i--) {
                        var p = random(i+1);
                        var t = this ;
                        this
                        = this[p];
                        this[p] = t;
                        }
                        return this;
                        };
                        ASSetPropFlags(Array.prototype, "shuffle", 1, 1);

                        And what is the ASSetPropFlags for?

                        Thanks,

                        Sam
                        • 9. Re: Generating random numbers that don't repeat
                          Rothrock Level 5
                          No. I didn't forget a line. I use it just like this:

                          myArray.shuffle();

                          That will shuffle the array – in place as it were. I guess adding the return isn't a bad idea, but then you will end up with two shuffled arrays. And because flash duplicates arrays by reference you might find a few surprises down the line.

                          The ASSetPropFlags is used to "hide" the new prototype from the tyranny of the for…in construction.

                          Create an array and do this:

                          for(a in myArray){
                          trace(a+" is "+myArray[a])
                          }

                          Try it both with that line and with that line commented out. I think the benefits will be clear.