4 Replies Latest reply on Feb 19, 2010 5:55 AM by Ned Murphy

    Random Name Combination Generator

    hjfergusson Level 1

      Hello, I am currently putting together a simple game where people have to pass my tests to join a club. At the end I would like them to be given a unique name. I imagine this could be done if I make 3 lists of 50 words, and right a piece of code that randomly selects one word from each list to create a unique combination almost every time. I am using actionscript 2 because I am fairly novice and this is about as complicated as my code is going to be, but is this to bigger job for action script 2?




        • 1. Re: Random Name Combination Generator
          RossRitchey Level 4

          Not at all.


          You simply need to drop those three lists into Arrays, and select from a random index:


          Here is a simple example, that sets up 3 Arrays and randomly picks one entry from each.  I placed it in a loop that traces out the resulting name, and repeats 100 times to show that it is working.


          var list1:Array = ["Name1","Name2","Name3"];
          var list2:Array = ["Name1","Name2","Name3"];
          var list3:Array = ["Name1","Name2","Name3"];
          for(var i:Number = 0 ; i < 100 ; i++){
               var randNum1:Number = Math.round(Math.random()*(list1.length-1));
               var randNum2:Number = Math.round(Math.random()*(list2.length-1));
               var randNum3:Number = Math.round(Math.random()*(list3.length-1));
               var randomName:String = list1[randNum1]+list2[randNum2]+list3[randNum3];



          If you drop 50 names into each of those Arrays, you should have a sufficiently random name generator, though I would lean towards more names per Array, the more names the less chance of duplication of names.


          You will also want a check to make sure the randomly generated name hasn't already been used, but that is simple enough to do.

          • 2. Re: Random Name Combination Generator
            Ned Murphy Adobe Community Professional & MVP

            This is no problem for Actionscript.  You can use the Math.floor() and Math.random() methods together to randomly select an item from an array...


            Let's say you have an array named myArray.... the first line below randomly selects one item from it...


            selectedItem = myArray[Math.floor(Math.random()*myArray.length)];



            While the probability might be somewhat low, it is very possible to have the same names assigned to different people, so you should have something that checks against all the assigned names to make sure a duplication does not occur.... if so, run the selection process again.

            • 3. Re: Random Name Combination Generator
              RossRitchey Level 4

              Nice, we posted a response at the exact same time

              • 4. Re: Random Name Combination Generator
                Ned Murphy Adobe Community Professional & MVP

                The previous forums made it neater because they showed the seconds as well.