5 Replies Latest reply on Nov 20, 2008 11:07 AM by Rothrock

    Array Question

    Level 7
      Dear All

      I have an array that I wish to randomise but randomise within a given
      constraint. I have an array of 0's and 3's. In the abbreviated array, for
      demonstration purposes, I have only 2 entries for 3 and the rest are 0's. I
      am using the code below. My issue is that any element could be a 0 but only
      5 could be a 3 but I can only have 2 x 3's in the array. For example the
      last element could be a 0 or a 3 but the last element can only be a 0. How
      could I create an array made up of 0's and 3's as described above? It's
      almost like saying you can radomise all elements in the array except 5 which
      must be 0, the remaining 5 are randomised and only 3 of these can contain a
      3.

      Array.prototype.randomize = function() {
      var i = this.length;
      if (i == 0) return;
      while (--i) {
      var j = Math.floor(Math.random()*(i+1));
      var tmp1 = this ;
      var tmp2 = this[j];
      this
      = tmp2;
      this[j] = tmp1;
      }
      return this;
      };
      mrmonopoly = new Array(0, 3, 0, 3, 0, 0, 0, 0, 0, 0);
      mrmonopoly.randomize();

      Thanks. I hope you understand what I mean.

      Alastair

        • 1. Re: Array Question
          Ned Murphy Adobe Community Professional & MVP
          Try reading what you wrote and see if it makes sense to you. It doesn't to me, so I'm at a loss to try to help. You keep changing the number of 3's allowed, which is the primary issue I have, but there's other disconnects as well.
          • 2. Re: Array Question
            Level 7
            Thanks for that NedWebs,

            I noticed that as soon as I posted and I thought I would further 'muddy the
            waters' with more comments. I would say in summary, how can you randomise an
            array and say a few elements in the array have to be a certain value. The
            number of 0's or 3's are not important. The quantities of each was by way of
            a demonstration. It is more the coding structure help I am looking for.

            Thanks again.

            Alastair

            "NedWebs" <webforumsuser@macromedia.com> wrote in message
            news:gg3t37$djk$1@forums.macromedia.com...
            > Try reading what you wrote and see if it makes sense to you. It doesn't
            > to me,
            > so I'm at a loss to try to help. You keep changing the number of 3's
            > allowed,
            > which is the primary issue I have, but there's other disconnects as well.
            >

            • 3. Re: Array Question
              Rothrock Level 5
              Okay. Two things. One you asked for and one you didn't. First the one you didn't.

              If you use the prototype approach here, you will be adding an item to every Array instance. And if you ever try and use a for(a in myArray) construction you will find that you will get one more item than you thought and it will most likely break. (There are also all kinds of problems with listeners which use an array behind the scenes!) So you should hide your prototype from the for loop. Put this line after the definition of your randomize method.

              ASSetPropFlags(Array.prototype,["randomize"],1,1);

              The only way I know to do what you have said is to do it. Depending upon the exact number of quanities and restrictions your approach will vary. For example I often have a list of maybe ten things. I shuffle it and when I get to the end I want to cycle through again. But I want to sure that the last item from the previous go through isn't the same as the first two items of the next go through. In that case I create a do...while loop around my randomize call. So if I wanted to be sure that position 5 held a three:

              do{
              mrmonopoly.randomize()
              } while (mrmonopoly[5]!=3)

              Of course you have to figure out what are the odds that it is going to have to execute multiple times. This method is fast, but for really large arrays or if the odds of getting a "wrong" sort are high it could slow you down quite a bit.

              So for the example you gave above. I might create an array with one 3 and the rest 0. Shuffle it until position 5 isn't a 3. And then set postion 5 to 3.

              If you start having lots and lots of condtions. Then you really aren't looking for a random sort and there are probably otherways to generate what you are looking for.
              • 4. Re: Array Question
                Level 7
                Rothrock,

                Thank you very much for the comprehensive answer you have provided me. I
                appreciate the effort you went to on my behalf. I understand the 2nd comment
                but I will need to look at the first part again as I think it is too
                advanced for me at the moment. I will do as you describe and try to
                understand it.

                Anyway thanks again for your help this Thursday tea time (UK).

                Alastair

                "Rothrock" <webforumsuser@macromedia.com> wrote in message
                news:gg452f$na9$1@forums.macromedia.com...
                > Okay. Two things. One you asked for and one you didn't. First the one you
                > didn't.
                >
                > If you use the prototype approach here, you will be adding an item to
                > every
                > Array instance. And if you ever try and use a for(a in myArray)
                > construction
                > you will find that you will get one more item than you thought and it will
                > most
                > likely break. (There are also all kinds of problems with listeners which
                > use an
                > array behind the scenes!) So you should hide your prototype from the for
                > loop.
                > Put this line after the definition of your randomize method.
                >
                > ASSetPropFlags(Array.prototype,["randomize"],1,1);
                >
                > The only way I know to do what you have said is to do it. Depending upon
                > the
                > exact number of quanities and restrictions your approach will vary. For
                > example
                > I often have a list of maybe ten things. I shuffle it and when I get to
                > the end
                > I want to cycle through again. But I want to sure that the last item from
                > the
                > previous go through isn't the same as the first two items of the next go
                > through. In that case I create a do...while loop around my randomize call.
                > So
                > if I wanted to be sure that position 5 held a three:
                >
                > do{
                > mrmonopoly.randomize()
                > } while (mrmonopoly[5]!=3)
                >
                > Of course you have to figure out what are the odds that it is going to
                > have to
                > execute multiple times. This method is fast, but for really large arrays
                > or if
                > the odds of getting a "wrong" sort are high it could slow you down quite a
                > bit.
                >
                > So for the example you gave above. I might create an array with one 3 and
                > the
                > rest 0. Shuffle it until position 5 isn't a 3. And then set postion 5 to
                > 3.
                >
                > If you start having lots and lots of condtions. Then you really aren't
                > looking
                > for a random sort and there are probably otherways to generate what you
                > are
                > looking for.
                >

                • 5. Re: Array Question
                  Rothrock Level 5
                  Great. Even if you don't understand all the problems that could be caused by using this approach. Just add the ASSetPropFlags line after the definition of your prototype. Make sure the name in quotes exactly matches the name of the function (method) you have defined.

                  There is time later to understand what it all means. But you could end up very sad if you don't add that line. :)

                  Cheers and good luck!