error with Math.round(Math.random..... code

error with Math.round(Math.random..... code

Post by Meatoaf012 » Mon, 20 Feb 2006 02:13:47


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.yqcomputer.com/ %20numbers.fla

feel free to ask any questions. look forward to hearing from someone.
 
 
 

error with Math.round(Math.random..... code

Post by kgla » Mon, 20 Feb 2006 02:35:06

go.onRelease = function() {
var min = Number(minim.text);
var max = Number(maxim.text);
out1.text = min+(Math.round(Math.random()*(max-min)));
};

 
 
 

error with Math.round(Math.random..... code

Post by kgla » Mon, 20 Feb 2006 02:36:41

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

error with Math.round(Math.random..... code

Post by Meatoaf012 » Mon, 20 Feb 2006 02:39:42

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.
}
};

actually does? thanx a whole lot!
 
 
 

error with Math.round(Math.random..... code

Post by kgla » Mon, 20 Feb 2006 02:55:54

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
 
 
 

error with Math.round(Math.random..... code

Post by Meatoaf012 » Mon, 20 Feb 2006 03:25:12

thanks a lot! i finished it now, check it out http://www.yqcomputer.com/ , if you like.

(i edited the code you gave me to clear all the boxes, to make it generate to all boxes as well)
 
 
 

error with Math.round(Math.random..... code

Post by kgla » Mon, 20 Feb 2006 03:30:24

you're welcome. that looks nice.