Skip to main content
Participating Frequently
February 16, 2010
Answered

Creating a random number - possible?

  • February 16, 2010
  • 2 replies
  • 854 views

Hi All,

I have created a flash ad, that cycles through 3 different ads, in order, always starting at 1.

I'm trying to find script to generate a random number, say between 1 and 3. Then, based on that number, it will start playing at a predetermined frame. For instance:

If RND# = 1 start playing at frame 7

If RND# = 2 start playing at frame 650

if RND# = 3 start playing at frame 1250

Can this be done? Does anyone have any code for this?

Thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

You can use the Math.random() method to generate a value between 0 and 0.999999..... and mutliply that by the number of values you want to allow for (3) in your case.   You can then round this random number down using the Math.floor() method to get an integer value.  So in this case you could end up with 0,1,2.  Which is probably good enough for your intentions, though you could add 1 to it if you really wanted 1,2,3.

var randomNumber:uint = Math.floor(Math.random()*3);

You could then have an array with your three vallues populating it and use the randomNumber to specify which is used in a gotoAndPlay() command...

var startFrames:Array = new Array(7, 650, 1250);

gotoAndPlay(startFrames[randomNumber]);

2 replies

kglad
Community Expert
Community Expert
February 16, 2010

you can use randF(1,3) to return a random integer between 1 and 3.  and you can use an array to associate your random numbers with particular frames:

var frameA:Array=[null,7,650,1250];

function randF(n1:int,n2:int):int{

var ul:int = Math.max(n1,n2);

var ll:int = Math.min(n1,n2);

return Math.floor(Math.random()*(ul-ll+1))+ll;

}

Ned Murphy
Ned MurphyCorrect answer
Legend
February 16, 2010

You can use the Math.random() method to generate a value between 0 and 0.999999..... and mutliply that by the number of values you want to allow for (3) in your case.   You can then round this random number down using the Math.floor() method to get an integer value.  So in this case you could end up with 0,1,2.  Which is probably good enough for your intentions, though you could add 1 to it if you really wanted 1,2,3.

var randomNumber:uint = Math.floor(Math.random()*3);

You could then have an array with your three vallues populating it and use the randomNumber to specify which is used in a gotoAndPlay() command...

var startFrames:Array = new Array(7, 650, 1250);

gotoAndPlay(startFrames[randomNumber]);

Participating Frequently
February 17, 2010

I'm very new at Flash scripting - so here's what I did:

I inserted the below code into frame 1 in the action script:

var randomNumber:uint = Math.floor(Math.random()*3);
var startFrames:Array = new Array(2, 470, 905);
gotoAndPlay(startFrames[randomNumber]);

I figured out that I need to be running AS 3, not 2....so this change was made....

It doesn't seem to work though - no matter what, it just plays through frame 1 till the end, then repeats....

Am I missing something? And how can I make this happen only 1 time? In other words, when the page loads, it should use the code above to determine a random start point....but then allow the ads to play in order after that....

Any idea?

Ned Murphy
Legend
February 17, 2010

The only thing I can think of is that you don't have any frames out as far as the frames in your array so if it selects a higher frame than what you have content in it immediately heads back to the start.  In the last frame of your timeline it should have a gotoAndPlay(2) in it, unless you want to return to frame 1 and randomly pick again.  Include a trace command to see what the values are just before the gotoAndPlay line you show...

var randomNumber:uint = Math.floor(Math.random()*3);
var startFrames:Array = new Array(2, 470, 905);

trace(randomNumber,startFrames[randomNumber]);
gotoAndPlay(startFrames[randomNumber]);