12 Replies Latest reply on Oct 20, 2006 4:25 AM by qootz

    How to make a random name appear

    anubia Level 1
      I have about 60 names and I need one to randomly appear later in my movie. I need it in a movie clip since i need it to fade up, be onscreen, then fade down. I think I need to make some sort of array and have a random function to pass the name to the movie clip, but I don't know how to do this. Is there a tutorial anywhere or a module I can insert?
        • 1. Re: How to make a random name appear
          DazFaz Level 1
          var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton and the wheelies");
          callMeaName = function(){
          return myNames[random(myNames.length)];
          }
          trace(callMeaName())
          • 2. Re: How to make a random name appear
            Level 7
            First I was surprised it did work, then I thought of the old random
            function.
            the random function is deprecated. Math.random is the way to go now.
            Using it would result in code below:

            var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton
            and the wheelies");
            callMeaName = function(){
            return myNames[Math.floor(Math.random()*myNames.length)];
            }
            trace(callMeaName())

            Lengthier? Yes... (But it'll last longer :)
            Too bad they don't give dates or version numbers when something will be
            phased out instead of just deprecated.

            Manno


            DazFaz wrote:
            > var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton and the wheelies");
            > callMeaName = function(){
            > return myNames[random(myNames.length)];
            > }
            > trace(callMeaName())

            --
            ----------
            Manno Bult
            manno@xs4all.nl
            • 3. Re: How to make a random name appear
              DazFaz Level 1
              The problem with using Math.foor is that it will only round down to the nearest number and not give a true random number in all acounts:

              Manno wrote:
              >var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton
              and the wheelies");
              >callMeaName = function(){
              >return myNames[Math.floor(Math.random()*myNames.length)];
              }
              >trace(callMeaName())

              >Lengthier? Yes... (But it'll last longer :)
              >Too bad they don't give dates or version numbers when something will be
              >phased out instead of just deprecated.

              To create a true random number you should use Math.round(). This will round up or down to the nearest integer:

              var myNames:Array = new Array("bob", "fred", "Mr Smith", "noddy", "Charlton and the wheelies");
              Array.prototype.randomThis = function() {
              s = this[Math.round(Math.random()*(this.length-1))]
              return s
              };
              trace(myNames.randomThis());

              Manno was nearly there and is right about using code that will not be outdated to soon.

              DazFaz
              • 4. Re: How to make a random name appear
                Level 7

                Hi DazFaz,

                was browsing and found your reply...

                In what way do you mean: "will not give a true random number in all
                acounts"?

                Is that something internal to the rounding of numbers?

                Although ofcourse not the same in result, both methods _seem_ pretty
                random to me:

                var ar:Array = new Array("random", "floor", "ceiling");
                var rnd:Number = Math.random();
                var rndV1 = Math.floor(rnd*ar.length);
                var rndV2 = Math.round(rnd*(ar.length-1));

                trace(rnd + "\t" + rndV1 + "\t" + rndV2);


                kind regards,
                Manno

                DazFaz wrote:
                > The problem with using Math.foor is that it will only round down to the nearest
                > number and not give a true random number in all acounts:
                > >return myNames[Math.floor(Math.random()*myNames.length)];
                > To create a true random number you should use Math.round(). This will round up
                > or down to the nearest integer:
                >
                > Array.prototype.randomThis = function() {
                > s = this[Math.round(Math.random()*(this.length-1))]
                > return s
                > };




                --
                ----------
                Manno Bult
                manno@xs4all.nl
                • 5. Re: How to make a random name appear
                  DazFaz Level 1
                  Hi Manno Bult.

                  The problem with user Math.floor is that it only moves in one direction and that is down to an integer.
                  With Math.round ir rounds off the number to the nearest integer.

                  If you run the code I have modified from yours, you will see what I mean. You will notice that rndV1 always stays at 0 yet rndV2 will deviate from 0 to 1. Making it a more accurate way of finding a true random number.
                  • 6. Re: How to make a random name appear
                    Level 7
                    Hi DazFaz,

                    true, but with an array with length of 1 rounding down prevents an index
                    out of range error.
                    In you original code you 'round down' by multiplying by the length of
                    the array -1 while I round down with Math.floor (and multiply by the
                    actual length of the array).

                    Is there a difference in result?

                    The code below can yield 1 while the array may just be 1 item long:

                    var arrayLength:Number = 1;
                    var rnd:Number = Math.random() * arrayLength;
                    var rndV1 = Math.floor(rnd);// Rounds ONLY down to the nearest integer
                    var rndV2 = Math.round(rnd);// rounds up OR down to the nearest integer

                    trace(rnd + "\t" + rndV1 + "\t" + rndV2);

                    so either decrement the length or floor the number. Same difference (No?)

                    I love puzzels :)


                    DazFaz wrote:
                    > Hi Manno Bult.
                    >
                    > The problem with user Math.floor is that it only moves in one direction and
                    > that is down to an integer.
                    > With Math.round ir rounds off the number to the nearest integer.
                    >
                    > If you run the code I have modified from yours, you will see what I mean. You
                    > will notice that rndV1 always stays at 0 yet rndV2 will deviate from 0 to 1.
                    > Making it a more accurate way of finding a true random number.
                    >
                    > var rnd:Number = Math.random();
                    > var rndV1 = Math.floor(rnd);// Rounds ONLY down to the nearest integer
                    > var rndV2 = Math.round(rnd);// rounds up OR down to the nearest integer
                    >
                    > trace(rnd + "\t" + rndV1 + "\t" + rndV2);
                    >

                    --
                    ----------
                    Manno Bult
                    manno@xs4all.nl
                    • 7. Re: How to make a random name appear
                      NSurveyor Level 2
                      DazFaz you've got it all backwards! Math.floor() WILL give you "true" random, while Math.round() won't!

                      http://www.quip.net/blog/2006/flash/choosing-random-numbers-redux
                      • 8. Re: How to make a random name appear
                        NSurveyor Level 2
                        To further demonstrate this:
                        • 9. Re: How to make a random name appear
                          Level 7

                          Ah!, that was the thing I was afraid of I had overlooked. Thanks.

                          Manno

                          NSurveyor wrote:
                          > DazFaz you've got it all backwards! Math.floor() WILL give you "true" random, while Math.round() won't!
                          >
                          > http://www.quip.net/blog/2006/flash/choosing-random-numbers-redux

                          --
                          ----------
                          Manno Bult
                          manno@xs4all.nl
                          • 10. Re: How to make a random name appear
                            DazFaz Level 1
                            Now I’ve got my teeth into this problem, and to prove my initial deposition, does anyone know how the Math.random() function works out its calculations? I have made a few calculations but before I can quantify these I need a better understanding of how this function comes to its final number. And more importantly to how many decimal places does it work to. Oh and why that many too.
                            • 11. Re: How to make a random name appear
                              Level 7

                              You'd have to find someone more harcore than me to get an anwer to your
                              first question... Although NS' explanation has to do with rounding by
                              floor, round or ceil and not so much with the random function. That is
                              used in all mentioned scripts.

                              And why does Math.random() yeild so much decimals? Dunno, how many bits
                              would it take to store such a number?

                              But why does it change the number of decimals:

                              0.976767854765058 0 1
                              0.88760749110952 0 1

                              above traces come from code below:

                              var arrayLength:Number = 1;
                              var rnd:Number = Math.random() * arrayLength;
                              var rndV1 = Math.floor(rnd);
                              var rndV2 = Math.round(rnd);

                              trace(rnd + "\t" + rndV1 + "\t" + rndV2);

                              Nice puzzle for the weekend :) (gotta get back to work...)

                              DazFaz wrote:
                              > Now I?ve got my teeth into this problem, and to prove my initial deposition,
                              > does anyone know how the Math.random() function works out its calculations? I
                              > have made a few calculations but before I can quantify these I need a better
                              > understanding of how this function comes to its final number. And more
                              > importantly to how many decimal places does it work to. Oh and why that many
                              > too.
                              >

                              --
                              ----------
                              Manno Bult
                              manno@xs4all.nl
                              • 12. Re: How to make a random name appear
                                qootz Level 1
                                thanks for all