A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Random numbers

  1. #1
    Senior Member
    Join Date
    Feb 2004
    Posts
    247

    Random numbers

    I was wondering if there was an easier way to select a random number between:

    -10 and 5 & 5-10

    like choose one of these:

    -10,-9,-8,-7,-6,-5,5,6,7,8,9,10

    I know an easy was would be:

    upordown = random number between 1 and 2

    if(upordown ==1){
    make random number between -10 and -5}
    else{make number between 5 and 10}



    Just wondering


    Thanks!

  2. #2
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    Code:
    trace(Number((random(2) == 1 ? "-" : "+")+(5+random(6))));
    New sig soon

  3. #3
    Senior Member
    Join Date
    Feb 2004
    Posts
    247
    Thanks. Wow thats pretty complicated.

  4. #4
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    well it only looks complicated because i have compressed it to one line. its actually pretty simple.

    i'll give you a quick run through.

    this bit:
    Code:
    (random(2) == 1 ? "-" : "+")
    is like a if else if statment, if random(2) is equal to 1 then it returns "-" else it returns "+".

    to put it in a more familliar format, it would look like this:
    Code:
    if(random(2) == 1){
        return "-"
    } else {
        return "+"
    }
    random(6) will return an integer between 0 and 5, therefore 5 + random(6) will give a number between 5 and 10,

    if you up this together with the "-" or the "+" you get a string which is either "-"5 to 10 or "+" 5 to ten.

    then all of that is wraped in a Number() function, the Number() function converts the string passed to it to a number, in this case the "+" or "-" 5 to 10.

    hope this helps you understand the code a bit better,

    zlatan
    New sig soon

  5. #5
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    the long winded way of doing it would be like this:
    Code:
    var randomNumber:Number;
    if(Math.round(Math.random()*2) == 1){
    	randomNumber = 0-(5+Math.round((Math.random()*6)))
    }else{
    	randomNumber = (5+Math.round((Math.random()*6)))
    }
    trace(randomNumber)
    note that i use Math.random() and not random() this is because random() is deprecated.
    if you want to get integer values from Math.random() remember to use Math.round(),

    HTH,

    zlatan
    New sig soon

  6. #6
    Senior Member flashfriend8's Avatar
    Join Date
    Oct 2004
    Posts
    146
    Code:
    while(rand > -5 && rand < 5)
    {
         rand = random(20)-10;
    }
    I have no idea where this is going.

  7. #7
    Senior Member
    Join Date
    Feb 2004
    Posts
    247
    Thanks! Are any of these more effecient than others?

  8. #8
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    i would say that
    Code:
     trace(Number((random(2) == 1 ? "-" : "+")+(5+random(6))));
    is the most efficient than the others because it does it in one go with no loops plus its only one line.

    but you use whichever you find easiest.

    zlatan
    New sig soon

  9. #9
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    Flashfriend8's method is probably the least efficient, as it employs a trial and error type method, whereby Flash just keeps generating random numbers until we get something in the required range. Dudeqwerty is getting there, but you still have to do some conversions from strings to numbers, which isn't a very efficient thing to do. The simplest solution seems to be this one:

    r = (random(6) + 5)*(1-2*random(2));

    Basically, you create two random numbers. The first number is between (and including) 5 and 10. The second number is either 1 or -1. Then you multiply them together. This is similar to dudeqwerty's, but I've used 1 and -1 instead of the symbols '+' and '-'.

    This turns out to be the fastest method overall, but we're talking about the difference of a few millionths of a second. However, this method is also the easiest to write down and understand, which is why it's the one I would use.

    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  10. #10
    Senior Member
    Join Date
    Feb 2004
    Posts
    247
    Ya, that is simple. thanks

  11. #11
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Another way: Put all the values you want to select from in an array:

    var numbers:Array = new Array(-10, -9, -8, -7, -6, -5, 5, 6, 7, 8, 9, 10);

    Then use Math.random() to choose a random array index. In this case, you need a number between 0 and 11.

    var randIndex:Number = Math.floor(Math.random() * 12);
    var randNumber:Number = numbers[randIndex];

  12. #12
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    yeah nice one dmonkey.

    but mazoonist, i really wouldnt use your method, your are just using more memory than is necessary. also i think it would be the slowest as flash has to generate a random index then go to the array and read the value in the index.

    zlatan
    New sig soon

  13. #13
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    OK, since dude threw in the comment about timing I thought I'd chip in my two cents.

    From past experience with other programming languages, it stands to reason that indexed lookups should be the fastest as they require the least amount of CPU instructions to execute. But, before embarassing myself with comments like that, I decided to write up a simple test harness to compare things such as timing, distribution of numbers, and validity of results from the solutions presented. I also decided to change the functions from using random() to Math.random() and so forth. Take a look at the attached zip file which contains a Flash 8 and a Flash MX 2004 version of the script. I'd be interested in seeing if anyone else is seeing the same results as I am.

    First and foremost (as expected) random() is much faster than Math.random() is any day of the week. All of the functions seems to provide the same level of "randomness" in the distributions; between 8% and 10% for each valid number, which is to be expected with 12 valid numbers (100 / 12 = 8.33333). However, what did surprise me is to look at the timing between the three solutions. Of course, I don't have a machine dedicated to testing so the numbers fluctuate all over the place due to background tasks and so forth; however, the general trend that I'm seeing is that dmonkey's solution seems to run the fastest in most cases, Mazoonist's comes in a very close second, and dude's falls behind, but not by much. In all cases, run times were less than 100 ms for 1,000 tests; which means that a single pull for any of these three is less than 0.1ms!
    Attached Files Attached Files
    If you can read this, you're in the right place.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center