7 Replies Latest reply on Jan 30, 2010 9:05 PM by Flex harUI

    Generating N random Number

    hitesh.rawat

      Hi,

       

      needed some help regarding Generating N random numbers .............this is what i did.....

      __________________________________________________________________________________

       

      <mx:Script>
              <![CDATA[
                          
                  private function Init(event:MouseEvent):void
                  {
                 
                  var i:int;
                  var randomNumber:Array;
                  for (i = 0; i<6; i++)
                  {
                 
                  randomNumber[i] = randomRange(0, 100);
                  test.text = randomNumber[i];  ------- ISSUE-----------------
                  }

        }
                 
                  public static function randomRange(low:Number, high:Number):Number
                      {
                          return Math.floor(Math.random() * (high - low)) + low;
                          
                      }
                 
                 
              ]]>
          </mx:Script>

      __________________________________________________________________________________

       

      I wanted to check what are the numbers being generated, so i tried putting the RandomNumnber Array Equal to the text property of the Text Area as we don't have trace option in Flex.

      However, nothing is getting printed ...........

       

      somebody help me with this.....................

        • 1. Re: Generating N random Number
          RUSH-ME Level 3

          Hi

           

          Initialize the array like

           

          var randomNumber:Array = [];

           

          remove the math.floor function from your randomRange() function.

           

          Hope this helps.

          Rush-me

          • 2. Re: Generating N random Number
            hitesh.rawat Level 1

            Hi, thanks for the help.....that seems to be working.......

             

            however what to do to print the whole array?????

            • 3. Re: Generating N random Number
              RUSH-ME Level 3

              Hi

               

              You can use Alert.show() or trace() to show the array values.

              Use for loop if required.

               

              rush-me

              • 4. Re: Generating N random Number
                hitesh.rawat Level 1

                well i'm doing this is flex.....and flex doesn't have trace().

                 

                I'm trying to randomize the index number while calling nodes from a XML file, that is the reason, i'm trying to put in a random number generator...cause number of nodes in XML are fixed. So with a random and unique number, i can call, say, 6 nodes from the XML without repeating them.

                 

                so, i wanted to check the if the random function repeats vaules? thus want to print the values......

                 

                this is getting confusing.......thanks for the help...however can you give something simple......so that it can help with my next goal of using the value in place of Index .....?????

                • 5. Re: Generating N random Number
                  Flex harUI Adobe Employee

                  There is a trace() method in Flex.

                   

                  Math.rand() will eventually repeat.  Any random number generator will.

                  That's the point of being random, it can be any value, even one you've seen

                  before.

                   

                  If you want to guarantee non-repeating numbers, you need to pull numbers out

                  of a sequence randomly and deal with collisions.

                  • 6. Re: Generating N random Number
                    hitesh.rawat Level 1

                    Hi,

                     

                    I have tried it again, though didn't get the result from the trace();. Am i using it correctly here???????

                    I have this function which checks for the uniqueness of the element in the array, if they are not unique, it pops the element. I wanted to check if it is behaving the way i'm expecting it to be.....however i don't know if i'm using the trace option at the right place or why is it not showing the values??????

                     

                    __________________________________________________________________________________________ ____

                     

                    import mx.logging.targets.TraceTarget;
                               
                                private function Init(event:MouseEvent):void
                                {
                               
                                var RandomArray:Array = [];
                               
                                var i:int = 0;
                               
                                 for( var i:int = 0; i< 6; i++)
                                {
                               
                               
                               
                                RandomArray[i] = randomRange(0, 6);
                               
                                if(RandomArray.length > 1)
                                {
                               
                                        for( var a:int = 0; a<RandomArray.length; a++)
                                        {
                                       
                                                for(var b:int = a+1; b< RandomArray.length; b++)
                                                {
                                               
                                                        if( RandomArray[a].Equals(RandomArray[b]))
                                                                {
                                               
                                                                    RandomArray.pop( );
                                                                    i--;
                                                  
                                                                }
                                               
                                                }
                                       
                                       
                                        }
                                       
                                }   
                               
                               
                                }
                                trace(RandomArray);
                               
                                }

                     

                    __________________________________________________________________________________________ ____

                    • 7. Re: Generating N random Number
                      Flex harUI Adobe Employee

                      Trace outputs to the debug console.  It is not related to mx.logging.*

                      package which you can use if you want more control over the output.