PDA

Click to See Complete Forum and Search --> : Simple Random-Round function?

Ahhhk
09-02-2002, 08:22 PM
Ok, this one is ood, but probably REAL simple.

I need to come up with a random number between 97 and 98.5 and round it to one decimal place.

ex: 97.6

TIA

Ahhhk!

ericlin
09-03-2002, 01:06 AM
get a random number between 970 annd 985 then divide it by 10;

Dickee
09-03-2002, 01:37 AM
What ericlin posted is essentially correct ... this post steps through the process, and also takes care of the integer results for 97 and 98:

x = Math.round(((Math.random()*1.5)+97)*10)/10;
if (x.toString().length==2) x+=".0";
trace(x);

////////////////////////

// Math.random results in a decimal value between 0-1.
// Multiplying that value by 1.5 results in a decimal value between 0-1.5.
// Adding 97 results in a decimal value between 97-98.5.
// Multiplying by 10 moves the decimal point 1 to the right.
// Math.round results in the new 'tenths' decimal determining the new 'ones' rounding.
// Dividing the rounded number by 10 gives the final result.
x = Math.round(((Math.random()*1.5)+97)*10)/10;
// Since Flash drops superfluous '0's, resulting in an integer condition for
// values 97 and 98, create a string literal and add ".0" to represent tenths.
if (x.toString().length==2) x+=".0";
// Check the trace results with a 2 frame movie.
trace(x);

Richard
[Edited by Dickee on 09-03-2002 at 01:44 AM]

Ahhhk
09-03-2002, 02:10 AM
You guys rock! Thanks!