Random numbers between 10 and 20

How do I get random number between a range?\r\rMeaning: I want to pick a random number between 10 and 20, for example.\r\rLater, I may want a random number between 50 and 100, obviously excluding all numbers <50.\r\rAny thoughts?\r\r\r\rMY name is Inigo Montoya… You killed My father. Prepare to die!

random(10)+10;\r\ror with fancy syntax:\r\rMath.floor(Math.random()*10)+10\r\rwhat you multiply by determines the range, and what you add determines where the range falls.\r\rthose will yield 10 … 19, but never 20. if you’d like to include 20 (and have a spread of 11), change the multiplier (and thus the range) to 11.

thanks a million!\r\rHere’s a follow up question. I just love this stuff…\r\rI have 5 dynamic boxes, which I am filling with 5 random numbers. (using an array…)\r\rhow do I get the code to give me 5 random numbers, but not give me a duplicate number?\r\rThoughts?

this one comes up a lot…\r\rif you’re dealing with a smallish range, you can put your numbers into an array and splice them out randomly … thus, no duplicates.\r\rhow about building a function that returns the array of random numbers? this would go in frame 1 (or something):

  \r\rfunction getRandomNum(range,place,l){\r\r   // build the array of "range" length at "place"\r\r   var sourceArray = [];\r\r   while(range--){ sourceArray.push(place+range); };\r\r   // splice out "l" numbers\r\r   var randomArray = [];\r\r   while(l--){ randomArray.push(sourceArray.splice(random(sourceArray.length),1)); };\r\r   return(randomArray);\r\r}

\rthen assuming you want 5 numbers between 10-21:

  \r\rsomeRandomNumbers = _root.getRandomNum(10,11,5);

I am going to have to dissect that puppy to understand. You just took my array understanding, slapped it, and called it Betty. And I happen to love arrays.\r\rThanks for the response. I will try that whilst I masticate the code… hmmmm… chewy.\r\rI will admit I am lost… I will ponder on the following…\r\rhow does pushing numbers into an array make it avoid duplicates?\r\ror Where in the code am I avoiding a duplicate?

In fact, Supra created an array containing all the numbers you need, and then he spliced all the numbers of the array into another array. To understand difficult it is. Hmmm ? Hmmm ?\r\rpom 0]

it’s the splicing that avoids duplicates.\r\rwhen you call getRandomNums(10,11,5), initialArray looks like this:\r\rinitalArray = [11,12,13,14,15,16,17,18,19,20];\r\rthen you build random array by splicing a random index out of initialArray. so say random(initialArray.length) came up as 6, you’d splice 17 from initialArray and add it to randomArray. then your arrays would look like this:\r\rinitialArray = [11,12,13,14,15,16,18,19,20];\rrandomArray = [17];\r\rand so on for a total of 5 times.\r\rit’s not really that complicated, just that one nasty line of array madness. :wink:

I’m still spinning out of control. It works like a charm though…\r\rStill would love to understand it, since I get into these things so I may learn… I guess the splicing thing is totally foreign to me.\r\rI understand this:\r\r Give me a random number\r push the number into the array\r\rIm lost when it picks the random number, how does it extract non duplicate numbers?\r\rI should have taken advantage of the A.S.L classes (A/s as a second language), then again… A.S.L sounds funny, maybe that’s why I didnt take it… they’d be saying “you’re one of the ASL kids?” heheh…

thanks Supra… It definitely works like a charm…\r\rI will check and recheck until I get it right.

The script doesn’t avoid duplicates, it’s just that you take elements from the initial array all the time, and as you remove the values from the initial array when you put them in the other array, you cannot put them once more. Tricky :smiley: but clever. It destroys the array though…\r\rYou can also find algorithms that will do the job for you. I can’t remember the address right now.\r\rpom 0]

Ah… YES! YES! I see the light. It is BEAUTIFUL!\r\rThank you, guys!