
random numbers
how to pick out 5 random different numbers from 22 numbers?


Mod
Here:
myNumber = [];
for(m=0;m<=4;m++){
myPick = Math.round(random(22));
pushed = myNumber.push(myPick);
if(pushed>=5){
for(i=0;i<=4;i++){
trace(myNumber[i]);
}
}
};
 The right of the People to create Flash movies shall not be infringed. 

If it was that easy my friend!
Problem with your above code, is that you'll often get some same 2 numbers (and if you were lucky enough you could get the same 5 numbers!), and that basically means you're not allways getting 5 "unique random" numbers!
You must check if in the generated numbers if there are no duplicates, and generate new different numbers, if there are some duplicates.

Senior Member
Here is an elegant way to solve this problem that doesn't require futzing with arrays. I first read about this years ago in Jon Bentley's "Programming Pearls" column. The algorithm is by the great Donald Knuth.
It very careful sets up the odds so that each number has the correct statistical chance of being selected.
One of the nice side effects of this algorithm is that the numbers are produced in sorted order. It can be used to select from arrays of other objects as well, such as imagenames, framelabels etc.
code:
// Choose N numbers from a list of M numbers (ranging from 0  M1)
M = 22;
N = 5;
for (i = 0; i < M && N > 0; ++i)
{
if (random(Mi) < N)
{
trace(i);
N;
}
}
If you wanted to stick these numbers into an array, you could do it this way:
code:
// Choose N numbers from a list of M numbers (ranging from 0  M1)
M = 22;
N = 5;
ary = [];
for (i = 0; i < M && N > 0; ++i)
{
if (random(Mi) < N)
{
ary.push(i);
N;
}
}
trace(ary);
Finally, if you want the numbers to start from 1 (to be in the range 1M) then change this line:
ary.push(i);
to this:
ary.push(i+1);

thank you very much.
//I can not understand the actionscript very well and have some questions,help me please,thanks advance.
// Choose N numbers from a list of M numbers (ranging from 0  M1)
M = 22;
N = 2;
for (i=0; i<M && N>0; i++) {//why N>0 ? N =2,2 >0.is it unnecessary?
if (random(Mi)<N) {
trace("M is:"+M);//output "M is:22"
trace("Mi is"+(Mi));//output "Mi is7",
trace("random Mi is:"+random(Mi));//output "random Mi is:5",5<2?or why does it run?
trace("i is"+i);//out put "i is15"
N;
}
}

Senior Member
Here's the algorithm you're asking about, with line numbers added.
code:
1. // Choose N numbers from a list of M numbers (ranging from 0  M1)
2. M = 22;
3. N = 2;
4. for (i=0; i<M && N>0; i++) {
5. if (random(Mi) < N)
6. {
7. trace(i); // we pick I
8. N;
9. }
10. }
Your questions:
>> on Line 4, why N>0 ? N =2,2 >0.is it unnecessary?
Although the loop starts with N equal to 2, everytime it gets a hit, it decrements N, in line 8. When N falls to zero, we have picked two random numbers, and can terminate the loop early.
>> output "random Mi is:5",5<2?or why does it run?
The algorithm insures that each time it picks a number, it uses the correct odds for picking it.
When it attempts to pick the first number, the odds are N/M or 2/22, so 2/22 of the time it will pick the first number. The way this is accomplished is by generating a random number in the range 0  21. If it's less than 2, we've met our 2/22 odds.
If it doesn't pick the first number, there are only 21 numbers remaining and the odds increase to 2/21 or 2/(M1). Each time it doesn't pick a number, the odds increase this way. If it were to get to the last 2 numbers without picking a number, it will always pick the last two numbers, and indeed the odds of picking the second to last number would be 2/2 or 100%.
If it DOES pick the first number, we decrement N, which causes the odds of picking additional numbers to decrease. The odds of picking the second number are then 1/21. And so on... If we pick all the numbers, N drops to zero, and the odds of picking any remaining numbers are 0 / something, or zero.
Last edited by jbum; 08082004 at 04:01 AM.

thank you for your patient explain.

World Kit Vote Holder
Beautiful nugget, jbum!

Thanks! Just what I was looking for.
I have a stupid question, though (I am a newbie:)...How can I use the script in a movie? Right now when I added the script to a button, and I test the movie, the random numbers are displayed in the output window...
I want the numbers to appear in the movie itself (in a certain font and size). For example, the user has to do something, and in the next scene only the random numbers are displayed.
How do I do that?
Thanks in advance!!!

setVariable
Sweet, very handy bit of code mate thanks for that.
2funzone, you can put it in a function like
Code:
function randList(M,N) {
ary = [];
for (i=0; i<M && N>0; ++i) {
if (random(Mi)<N) {
ary.push(i+1);
N;
}
}
return(ary);
}
and just call it like:
myText.text = randList(22,6) //when you want 6 numbers returned from a poss set of 22
Cheers again Jbum
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
