-
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!
-
get a random number between 970 annd 985 then divide it by 10;
-
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]
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|