[JAVAscript]Math.random()?

Results 1 to 4 of 4
  1. #1
    Account Upgraded | Title Enabled! Gwei is offline
    MemberRank
    Jul 2005 Join Date
    i live in a buLocation
    677Posts

    [JAVAscript]Math.random()?

    How effective is this fomula in getting
    1(inclusive) to 100(inclusive) value at random?

    Math.floor((Math.random()+0.01) * 100);

    Any suggestion for better one?


    Say, my math.random() gets 0.9999
    I +0.01 it, and it gives 1.0999
    then times 100, i get 100.99, after I math.floor it, it is 100 right? not 110.
    Last edited by Gwei; 24-09-10 at 07:28 AM.


  2. #2

  3. #3
    Ginger by design. jMerliN is offline
    MemberRank
    Feb 2007 Join Date
    2,497Posts

    Re: [JAVAscript]Math.random()?

    Code:
    function nrand(min,max){
      return Math.round(Math.random()*(max-min))+min;
    }
    Enjoy. This isn't uniform for values min and max, though.

    This is:

    Code:
    function unrand(min,max){
      var n = min-1;
      while(n != min-1 && n != max+1) n = nrand(min-1,max+1);
      return n;
    }
    Last edited by jMerliN; 25-09-10 at 06:24 AM.

  4. #4
    Account Upgraded | Title Enabled! Gwei is offline
    MemberRank
    Jul 2005 Join Date
    i live in a buLocation
    677Posts

    Re: [JAVAscript]Math.random()?

    Sorry for coming back late. will test this out now



Advertisement