A Flash Developer Resource Site

1. ## Random SWF

Hi,

I'm having trouble searching the forum (using Safari), so I will ask my question and apologize if it's been answered.

I want to get a random number, but then exclude one of those numbers. How do I do that? Here is my script:

random = Number(random(5))+1;
loadMovieNum("q_" + random + ".swf", 1);

Let's say I want to exclude the number 2 from being chosen. What can I do to that script to make sure it only chooses 1, 3, 4 and 5?

Thanks,

Yevri

2. maybe:
code:
```
random = Number(random(5))+1;
if(random != 2){
loadMovieNum("q_" + random + ".swf", 1);
}
```

just a thought....

3. Here are two methods:

1.
code:
```
i = random(4)+1;
if (i >= 2) i++;
```

2.
code:
```
choices = [1,3,4,5];
i = choices[random(4)];
```

* * *

There is also a very elegant way of solving the problem of choosing N numbers from a larger set of M numbers. Search the boards for 'donald knuth' to find some previous threads where I've posted this algorithm.

* * *

Some other notes:

A couple of problems with this line:

random = Number(random(5))+1;

It is best not use variables which have the same names as built-in functions. Instead of calling your variable 'random', try calling it 'r' or 'rnd'.

Secondly, your use of the Number() function isn't necessary - random(5) already returns a number.

r = random(5)+1;

4. ## Random Movie

Thanks! That works perfectly. I appreciate you taking the time.

Yevri

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap