5 Replies Latest reply on Aug 9, 2006 5:35 PM by Joe_Casey

    random() isn't random

      I have an ActionScript puzzle game that scrambles letters and positions. Certain results show up in a very non-random way, which can only happen if the random() function is repeating the same string of numbers (maybe 30 or so) on different runs of the program. Is there more to the random() funtion than is shown in the reference? A seed or something? Something about repeated calls?

      Please advise, I don't want to have to write my own random function.
        • 1. Re: random() isn't random
          inlineblue Level 1
          After some quick testing, I can't see any noticeable repetition in the numbers. Can you confirm that the numbers coming out of random() are actually repeating? Are you keeping enough digits from the numbers?
          • 2. Re: random() isn't random
            Joe_Casey Level 1
            Here is everything I've learned about this:
            For reference, the puzzle game is: http://actionpuzzles.com/Reassembler.html
            I play the game in a browserless flash player on my own pc frequently. I started noticing the problem when it seemed like I was seeing the same starting word (chosen from a list of more than 1000) with the same encoding from time to time. I traced the random number used to pick the word and the results seemed ok - a reasonable distribution from 0 to 1000. Note that whenever I use random() turn it into an int with something like: "ran = Math.floor(Math.random()*range)" where range covers the values I need.

            Besides seeing the same word, I get the same encoding, which means that I am getting the same sequence of random integers over a run of at least 65 calls. I'm looking at the integers I use, not the reals that random() provides, but I don't think that makes any difference.

            I am not sure, but I think this problem only happens with the first game after I start the flash player.

            I really don't have to background to prove anything about the randomness of the random() function, but unless I get some help with this I will have to write my own function, or change the random() result based on the clock, or something.

            Thanks for your assistance, Joe Casey
            • 3. Re: random() isn't random
              You will have to write some kind of seed of your own. Use flash.utils.getTimer() * the random(). Or UTC timestamp from a new Date().

              Most random generators are only psuedo random. But most languages also provide seed functionality to help get around it. Later,
              • 4. Re: random() isn't random
                inlineblue Level 1
                Ok, I was able to reproduce the problem by repeatedly running a test swf file in a standalone player. After a bunch of runs, I noticed that there were maybe only 5 or 6 "random" sequences generated at startup. It's probably not a bug in the random() method itself, it's just that they haven't provided a convenient seeding method (which could be considered a bug). I think Renaun's advice is the way to go.
                • 5. Re: random() isn't random
                  Joe_Casey Level 1
                  Ok, will do.

                  It might be a good idea to add some discussion to the random() description in the Flex Reference.

                  Thanks, Renaun and Inlineblue.