PDA

View Full Version : random numbers between 10 and 20



Iammontoya
May 20th, 2002, 12:55 PM
How do I get random number between a range?

Meaning: I want to pick a random number between 10 and 20, for example.

Later, I may want a random number between 50 and 100, obviously excluding all numbers <50.

Any thoughts?



MY name is Inigo Montoya.. You killed My father. Prepare to die!

suprabeener
May 20th, 2002, 04:19 PM
random(10)+10;

or with fancy syntax:

Math.floor(Math.random()*10)+10

what you multiply by determines the range, and what you add determines where the range falls.

those 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.

Iammontoya
May 20th, 2002, 08:07 PM
thanks a million!

Here's a follow up question. I just love this stuff...

I have 5 dynamic boxes, which I am filling with 5 random numbers. (using an array..)

how do I get the code to give me 5 random numbers, but not give me a duplicate number?

Thoughts?

suprabeener
May 21st, 2002, 01:14 PM
this one comes up a lot...

if you're dealing with a smallish range, you can put your numbers into an array and splice them out randomly ... thus, no duplicates.

how about building a function that returns the array of random numbers? this would go in frame 1 (or something):


function getRandomNum(range,place,l){

// build the array of "range" length at "place"

var sourceArray = [];

while(range--){ sourceArray.push(place+range); };

// splice out "l" numbers

var randomArray = [];

while(l--){ randomArray.push(sourceArray.splice(random(sourceA rray.length),1)); };

return(randomArray);

}
then assuming you want 5 numbers between 10-21:


someRandomNumbers = _root.getRandomNum(10,11,5);

Iammontoya
May 21st, 2002, 03:23 PM
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.

Thanks for the response. I will try that whilst I masticate the code... hmmmm.. chewy.

I will admit I am lost... I will ponder on the following...

how does pushing numbers into an array make it avoid duplicates?

or Where in the code am I avoiding a duplicate?

ilyaslamasse
May 21st, 2002, 03:39 PM
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 ?

pom 0]

suprabeener
May 21st, 2002, 03:42 PM
it's the splicing that avoids duplicates.

when you call getRandomNums(10,11,5), initialArray looks like this:

initalArray = [11,12,13,14,15,16,17,18,19,20];

then 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:

initialArray = [11,12,13,14,15,16,18,19,20];
randomArray = [17];

and so on for a total of 5 times.

it's not really that complicated, just that one nasty line of array madness. ;)

Iammontoya
May 21st, 2002, 03:45 PM
I'm still spinning out of control. It works like a charm though...

Still 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.

I understand this:

Give me a random number
push the number into the array

Im lost when it picks the random number, how does it extract non duplicate numbers?

I 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..

Iammontoya
May 21st, 2002, 04:02 PM
thanks Supra.. It definitely works like a charm..

I will check and recheck until I get it right.

ilyaslamasse
May 21st, 2002, 04:42 PM
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 :D but clever. It destroys the array though...

You can also find algorithms that will do the job for you. I can't remember the address right now.

pom 0]

Iammontoya
May 21st, 2002, 04:51 PM
Ah... YES! YES! I see the light. It is BEAUTIFUL!

Thank you, guys!