A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Simple Random-Round function?

  1. #1
    Senior Member
    Join Date
    Aug 2002
    Location
    Texas, USA, Earth
    Posts
    124
    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!

  2. #2
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    get a random number between 970 annd 985 then divide it by 10;

  3. #3
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    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]

  4. #4
    Senior Member
    Join Date
    Aug 2002
    Location
    Texas, USA, Earth
    Posts
    124
    You guys rock! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center