A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Generate a random number, to a certain decimal point

  1. #1

    Generate a random number, to a certain decimal point

    Howdy is there anyway to generate a random number (within a range), to a certain decimal point and then display it in a text box?

    I know how to do a full number, but am struggling to find out how you get random numbers with decimals.

    Thanks in advance.
    Yourz Trooly,

    PB
    www.twistedpancreas.com

  2. #2
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    hi Pancreasboy
    Code:
    var num
    newNum=random(100)
    num=Math.random()*newNum
    trace (num)
    yesterday there was some debate about using random(number) as it is an older function that may not exist one day. But it works in this case....does that help?

  3. #3
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Here is a pretty useful thread regarding random usage with Flash...

    http://board.flashkit.com/board/showthread.php?t=690615

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    edit: oops, didnt notice "decimal"
    Last edited by moagrius; 06-30-2006 at 05:22 AM.

  5. #5
    ok so heres the code im using atm

    function shuffle(){

    return Math.floor(Math.random() * 2);

    }

    var allNumbers=[];

    for(var i=10;i<=20;i++){

    allNumbers.push(i);

    }

    allNumbers.sort(shuffle);

    results=[];

    for(var r=0;r<1;r++){

    results.push(allNumbers[r]);

    }

    trace(results);

    display1.text=results[0];

    so instead of the *2 should i have return Math.floor(Math.random() * 100)/100;


    sorry im more of a designer rather than a programmer
    Yourz Trooly,

    PB
    www.twistedpancreas.com

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    here's a couple ways - the first is the commonly accepted approach (with some minor speed tweaks), but you'll find that it'll fail in flash with large numbers. the second's functionality is entirely within string manipulation show should always return an appropriate product
    code:
    Number.prototype.toFixed=function(a){
    var b=Math.pow;
    return (this|0)+"."+(b(10,a)+(0.5+(this-(this|0))*b(10,a))+"").substr(1,a);
    }

    Number.prototype.toFixed=function(a){
    var b=(""+this).split(".");
    b[1]=[0.5+(b[1].substr(0,a+1)*0.1)|0]+"";
    while(b[1].length<a)b[1]=b[1]+0;
    return b.join(".");};

    var A=123.23498620398;
    var B=0.236;
    var C=0.233;
    var D=100;
    trace(A.toFixed(5));
    trace(B.toFixed(2));
    trace(C.toFixed(2));
    trace(D.toFixed(18));


  7. #7
    God yeps's Avatar
    Join Date
    Dec 2005
    Location
    :noitacoL
    Posts
    660
    the easyest way is just to go

    _root.ranDecimal = (random(1000000))/1000

    that should find a random number between 0 and 100 and it will be to the tenthousandths place (4 numbers past the decimal)

  8. #8
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    Here's a nice method that doesn't require string manipulation. The part you're interested in is how to get the required decimal places. The answer is simple! Mutliply the random number by 10^d (where d is the required number of decimal places), take the floor, and then divide by 10^d. For exmaple, let's say our number is:

    354.5679483722;

    and we want to 5 decimal places. Mutliply by 10^5 (100000):

    34556794.83722;

    take the floor:

    34556794;

    and then divide by 10^5:

    345.56794;

    Here's the function:

    code:

    function rnd(a:Number, b:Number, d:Number):Number {
    //Get a random number;
    var r:Number = Math.random();
    //Put the number in range;
    r = r*(b-a) + a;
    //Round to the required number of decimal places;
    r = Math.floor(r*Math.pow(10,d))/Math.pow(10,d);
    //Return r;
    return r;
    }



    The syntax is rnd(lowerLimit:Number, upperLimit:Number, decimalPlaces:Number). Let's say you wanted a number between 120 and 270 at 6 decimal places:

    var myRandom:Number = rnd(120, 270, 6);

    Hope this helps.

    NB: The method I use here means that the entire number is restricted to 15 significant figures. If you want more, let me know. There'll be a work around!
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  9. #9
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    those dont pad.

    you need a string, not a number, to get back 103.20 (e.g., $103.20 versus $103.2)

  10. #10
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    ... and string manipulation is faster than 3 lookups to the Math class

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