4 Replies Latest reply on Jun 25, 2008 3:26 AM by Newsgroup_User

# How do I make a range of numbers?

I am creating a flash game, and I can't seem to get a range to work correctly. I have an object that needs to be collected. once you touch it, it randomly regenerates on the y axis. That all works fine, however I'd like it to regenerate between lets say 50 and 400 because otherwise it overlaps a menu bar.

I think I could use the Math.random() method, but I can't figure how to get it to work in the sense I'd like it to. What should I change what I have to?

• ###### 1. Re: How do I make a range of numbers?
Math.random() in the Help to find the function he mentioned:

--
Dave -
http://www.blurredistinction.com

• ###### 2. Re: How do I make a range of numbers?
Sorry - I meant to actually paste in that function:

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) +
min;
return randomNum;
}

and you just use it like:

_root.point._y = randRange(50, 400);

--
Dave -
http://www.blurredistinction.com

• ###### 3. Re: How do I make a range of numbers?
ok, thanks. I guess I just don't get how to apply it. In mine, I have a definable means for x and y.

x=0
y=the function?

A function declaration is not permitted there. So how should it read out? Where should I put it so that the function is defining y?
• ###### 4. Re: How do I make a range of numbers?
You declare the function in your frame script - frame 1 typically, and then
you can set your _y to a call to the function, because the function returns
a number. Something like this:

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) +
min;
return randomNum;
}

gotoAndPlay(1)
_root.point._x = (0);
_root.point._y = randRange(50, 400);

If you want to see the return of randRange, you can trace it:

trace(randRange(50,400));

--
Dave -