1. ## Random Intervals

I have a ball that moves a long on a motion guide and it loops. Instead of having it loop constantly, I want it to loop, but at different intervals, like it would pause a few seconds then play.
Anyone got any ideas? Thanks in advance.

2. two things you can do:

1) add extra frames to your MC so that the ball stays motionless for the amount of time you need.

2) Use action script to code in a pause, there is a thread on that here if you do a search (called something like "pause movie"). If you added this code to your MC then it could be more dynamic... ie the pause length could be determined by a var, etc.

Hope this helps....

Peace

3. 22
posts
Registered User
You could try using setInterval. Assuming your ball movieclip is called something like ball_mc and has a stop() somewhere to keep it from looping automatically.
Code:
```//set minimum and maximum interval values in seconds
tMin = 2;
tMax = 10;

function move() {
ball_mc.gotoAndPlay(1);

//reset random interval
clearInterval(moveInt);
t = Math.round((Math.random()*(tMax - tMin)) + tMin)*1000;
moveInt = setInterval(move, t);

}

//set initial interval
moveInt = setInterval(move, 500);```
Just change tMin and tMax to whatever values you want for your pauses. Each time it runs the move function it will reset the interval to some random value.

4. Thanks for the replies guys, I couldn't get your code to work Bembino, maybe it's something I did wrong, but I ended up using the pause code Ryall suggested and it works as well.

5. [ this post ]

ok, no offense but I see a lot of people here using Math.round with random and I figured Id just shoot off some stuff about that. I mentioned this in another thread too so Ill make this my final altruistic gesture of educational outreach on this topic.

Random Numbers in Flash.

Inititally there was (and still is) the simple and effective random(number) function which when given a number generates a pseudo random number from 0 up to but not including that number. Example: random(10); returns any one of ten random numbers between 0 and 9 inclusive. Because random is 0 based, it works well with arrays which are also 0 based. This makes obtaining a random array index as easy as random(arrayInst.length);

However, now random() is considered 'deprecated' and it is encouraged that Math.random() is used instead. Does this mean random() doesnt work? No, of course not. And the truth is, its a lot faster in calculating random numbers than Math.random() is. Should you ever need the simple operation of random(), use it. Its not like it will stop working in any of your flash movies anytime soon (if at all).

So enter Math.random(). Math.random() is different from random() in that no argument is given and the generated random number is a float from 0 upto but not including 1. This means Math.random() will give you a number between 0 and 0.999999999999999. Because of this, if you want a whole number you'll have to multiply this random number by a 'max' whole number from which your range is to be developed from. Since the max of Math.random() now is about 1, multiplying Math.random() by 10 would give you a number from 0*10 which is 0 to 0.999999999999999*10 which is about 10. Of course because Math.random() returns a float, you are going to have those possibly unwanted decimals returned along with your newly calculated range which you might not want. So what do you do? round...

Therein lies the problem. Using Math.round() WILL round your number and give you a range between 0 and that number inclusive (ie. 0-10 including 10), but suddenly the values given are no longer evenly 'random' By this I mean round will allow some numbers to be more likely to occur than others. The problem occurs in the endpoints of the range. Math.round() rounds a number to the nearest whole number meaning it takes those values <= .5 below the whole number and those < .5 above and rounds them. What this means is that our enpoints (0 and 10 in the example) only have 1/2 the chance of being returned since you never get and float below 0 or above 10 i.e. -.5 or 10.5 which would include part of the 'range' of that individualy number. Heres a little chart thingy to try to visualize chance comparisons

Code:
```[-1][-0.5][0.0][0.5][1][1.5][2][2.5][3][3.5][4][4.5][5][5.5][6][6.5][7][7.5][8][8.5][9][9.5][10][10.5][11]
______/\________/\______/\______/\______/\______/\______/\______/\______/\______/\______/\________/\______```
where you can see (maybe heh) how the range of 0 and 10 go beyond the range of the random span (in blue). So to correct this a shift is needed to more or less merge those 1/2's into a whole. This could be a shift up (Math.ceil) or a shift down (Math.floor).

What ceil will do to this range is round up for every random float generated. So given the example of Math.random()*10 we would get a range of 1-10.... theoretically at least. But if you'll remember Math.random() returns a random float 0 upto and not including 1. This means that random CAN return 0 and ceil on 0 will return 0 which will make the range 1-10 defunct. Though rare to get 0, its still possible. Plus, to be picky, since 1 itself is never returned, that makes 10 that less (however so slightly) likely to be gotten.

So that leads us to Math.floor(). Given any Math.random()*multiplier range, floor will produce a number between 0 upto and not including the multiplier JUST like the original random(number) function. Because the range is from 0 upto but not including the mulitplier, floor will always give an equal chance for each number in the range and doesnt have the possibility of getting hung up on an oddball value as ceil does with 0 because Math.random() never reaches 1 hence never reaching the multiplier.

Knowing that, the 'correct' method of using Math.random() to achieve a random whole number is

Math.floor(Math.random()*multiplier)

which gives you a number from 0 upto but not including the multiplier
Code:
```Math.floor(Math.random()*10); // 0 - 9
Math.floor(Math.random()*25); // 0 - 24
1 + Math.floor(Math.random()*50); // 1 - 50

randomRange = function(min, max){
return Math.floor(Math.random()*(max-min+1)) + min;
}
randomRange(10,50) // 10-50```
and thats that

6. whoa, that is the most insightful response I've ever recieved on this forum. I'm going to save this thread and use it as a reminder to use Math.floor to get true random numbers. Thanks for taking the time and explaining that Senocular.

8. 22
posts
Registered User
Hey Senocular. Thanks for the great explanation. I guess I've just never been super picky about what number got generated just as long as it was within that range somewhere. I'll be sure to use Math.floor(Math.random()*n) from now on.

9. yeah its not like it doesnt work with round, and chances are its perfectly fine for any kind of random your doing, its just not as much of a 'unified' random as it could be. I guess Im just picky sometimes

10. ## When will programmers think things through?

After reading senocular's very, very, very detailed explanation, it strikes me as funny: When will programers learn that other programmers want random numbers, and just throw away all the dancing around crap and make a built-in function: random(low,high) which creates a whole number no lower than low, and no higher than high? It's funny, but practically every program language around right now requires about 5-20 lines of code just to do that simple function! If you want it, as senocular pointed out, to be a fair distribution of numbers. Well, I guess I can't complain too much, being a programmer myself, (progamus developus, is the scientific name, I believe), I once made a GUI which included 5 sub-menus to reach the "Exit" option... It actually took someone else to point out that trying to close the program should not take 5 minutes to find the right option for me to realize the error in my ways... the point is, if there is one, as long as man creates, there will always be the other man (or woman) right behind the finish product who says: "Why didn't the guy who made this think things through?"

11. A very good reply from senocular... Added something to my brain too ... and thanx for posting this question too

12. 10
posts
Registered User
Creating new instances might be more of a down side of my approach, it is similar to how I did it in as2 with setInterval, i would be unsure if it impacts garbage collection, as it overwrites, these aspects are occasionally worth testing, but hopefully garbage collection will get fixed/improved.

13. 84
posts
Registered User
can you provide a sample of the work you did