Random Number with variable
- Started
- Last post
- 7 Responses
- nosaj
Hello Folks,
I'm trying to generate a random number in Actionscript. I want the limit of the random number generated to be defined by a variable. For some reason I just can't get this to work.
here's the code I've been using:
test = Math.floor(Math.random()*_root.n..._root.number is the variable and movie recognises it everywhere except in this code. Any help would be greatly appreciated.
- unfittoprint0
you should problably change the variable name from number to myNumber. number is part of Flash's actionscript syntax and there may exist some conflict when using variables wiht names similar to existent A'script functions...
- ave0
Place the limit variable within the brackets:
Math.random(myVar)?
- nosaj0
Thank for the quick suggestions, but unfortunately it still doesn't work. Do I need to somehow state the the variable number is an integer so the random function can work with it?
- unfittoprint0
your variable is problably considered a string. Let's say it's value it's 2..
myNumber= new Number(myNumber)
//just a test....
trace (2+myNumber)
//if it trace's 4 it recognizes myNumber value as a number.
- nosaj0
Thanks unfit. I'm so close - I'm getting a Nan error. Do you know how I can tell Actionscript to treat the variable as a number?
- unfittoprint0
like I posted before... if myNumber ='3'
newNumber = new Number(myNumber)
good look and remember: when in difficulties is good to trace every step of the process to know where the error is...
- nosaj0
Ah ha, I finally got it working. Thanks so much for your help today.