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,
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(),
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
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.
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!