Groups | Blog | Home
all groups > flash actionscript > february 2006 >

flash actionscript : error with Math.round(Math.random..... code



Meatoaf0123
2/18/2006 5:13:47 PM
hi,

im pretty new to actionscript, and this is really my first attempt at ever
coding in it... I'm trying to make a random number generator as coding
practice, and hitting some bumps.

i want the generator to generate numbers from a range. this range is declared
by the user in Input boxes.

the generator ignores the minimum number and generates below it, but will not
generate numbers above the highest number, which i find strange. here is the
code:


//Random Number Generator

//set variables. "min" = the text with instance name "minim". "max" = the text
with instance name "maxim"

var min = minim.text;
var max = maxim.text;

/*when button with instance name "go" is released, the funtion occurs.
This function generates a random number between "max" and "min"*/}

go.onRelease = function() {
out1.text = (Math.round(Math.random()*(max-min)));
};

/*when the button with instance name "end" is released, the function occurs.
This function clears all the textboxes listed (out1 to out21).
These are output textboxes that show the generated numbers*/

end.onRelease = function() {
out1.text = "";
out2.text = "";
out3.text = "";
out4.text = "";
out5.text = "";
out6.text = "";
out7.text = "";
out8.text = "";
out9.text = "";
out10.text = "";
out11.text = "";
out12.text = "";
out13.text = "";
out14.text = "";
out15.text = "";
out16.text = "";
out17.text = "";
out18.text = "";
out19.text = "";
out20.text = "";
out21.text = "";
};

(i am fully aware that i could have used an array to clear the textboxes...
but i dont know how to, lol. if you could explain, it would be greatly
appreciated.)

the .fla can be found http://www.cheezy-funnys.com/random%20numbers.fla

feel free to ask any questions. look forward to hearing from someone.
kglad
2/18/2006 5:35:06 PM
go.onRelease = function() {
var min = Number(minim.text);
var max = Number(maxim.text);
out1.text = min+(Math.round(Math.random()*(max-min)));
};
kglad
2/18/2006 5:36:41 PM
end.onRelease = function() {
for(var i=1;i<=21;i++){
_root.text="" //<-- if your textfields are on the _root timeline.
}
};
Meatoaf0123
2/18/2006 5:39:42 PM
thanx, erm, would you mind explaining what the code:

for(var i=1;i<=21;i++){
_root.text="" //<-- if your textfields are on the _root timeline.
}
};

kglad
2/18/2006 5:55:54 PM
that clears your textfields _root.out1, _root.out2 etc. flash uses brackets to
resolve strings into objects (if they exist). so,

"out"+1 is

"out1" and the string "out1" flash can resolve to an object if you use
brackets:

_root is

_root.out1
Meatoaf0123
2/18/2006 6:25:12 PM
thanks a lot! i finished it now, check it out http://www.cheezy-funnys.com/rng.swf, if you like.

kglad
2/18/2006 6:30:24 PM
AddThis Social Bookmark Button