Math.random syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Scheer

    Math.random syntax

    Hi.

    Reading about the Math.random method I saw that by default it
    generates between 0 and 1. To generate numbers between a greater range
    I can use these syntaxes:

    x = Math.random()/10
    x = Math.random()*1 0

    What is the difference between the two? I could not understand the
    role of the operators here.

    Thanks,
    Robert Scheer
  • Randy Webb

    #2
    Re: Math.random syntax

    Robert Scheer wrote:[color=blue]
    > Hi.
    >
    > Reading about the Math.random method I saw that by default it
    > generates between 0 and 1. To generate numbers between a greater range
    > I can use these syntaxes:
    >
    > x = Math.random()/10[/color]

    Divides the return from random() by 10, giving you a decimal form
    between 0 and .1

    [color=blue]
    > x = Math.random()*1 0[/color]

    Gives you a decimal integer between 0 and 10 by multiplying.
    [color=blue]
    > What is the difference between the two? I could not understand the
    > role of the operators here.[/color]

    One divides, one multiplies.

    <URL: http://www.jibbering.com/faq/#FAQ4_22 />
    might be a good resouce for you, the FAQ in its entirety but especially
    that section.


    --
    Randy

    Comment

    • Lee

      #3
      Re: Math.random syntax

      Randy Webb said:[color=blue]
      >
      >Robert Scheer wrote:[color=green]
      >> Hi.
      >>
      >> Reading about the Math.random method I saw that by default it
      >> generates between 0 and 1. To generate numbers between a greater range
      >> I can use these syntaxes:
      >>
      >> x = Math.random()/10[/color]
      >
      >Divides the return from random() by 10, giving you a decimal form
      >between 0 and .1
      >
      >[color=green]
      >> x = Math.random()*1 0[/color]
      >
      >Gives you a decimal integer between 0 and 10 by multiplying.[/color]

      No. It's not an integer.

      The correct way to generate a "random" integer is described at:


      Comment

      • Dr John Stockton

        #4
        Re: Math.random syntax

        JRS: In article <bu6rvg010ed@dr n.newsguy.com>, seen in
        news:comp.lang. javascript, Lee <REM0VElbspamtr ap@cox.net> posted at Thu,
        15 Jan 2004 12:08:16 :-[color=blue]
        >
        >The correct way to generate a "random" integer is described at:
        >http://www.jibbering.com/faq/#FAQ4_22
        >[/color]

        According to someone trustworthy, that method is not entirely reliable.
        From a linked page :

        Opera (5.02..6.01 at least), I have read, can give a value of 1.0
        from its Math.random(), with a frequency of the order of one
        in 35000 times - so that the function Random() below can
        return N. There is a
        <http://homepage.ntlwor ld.com/stephen.chalmer s/op6/random1.htm> Test
        age by SC. Precautions are needed; appending %1 to Math.random()
        should do. LRN 20030804 : Opera 4, 5, not 6.05.

        Sometimes, returning N may not much matter; other times, it may cause
        the page logic to fail, resulting in error but not necessarily obvious
        error.

        I do not know which values in 0..N-1 that 1/35000 probability is
        extracted from (it is transferred to 0, of course, by %1). If a 1/35000
        non-uniformity matters, ISTM that one should perhaps be using a better
        algorithm and maybe another language.

        function Random(N) {
        do { T = Math.random() } while (T>=1.0) ; return Math.floor(N*T) }

        may be better than

        function Random(N) { return Math.floor(N*(M ath.random()%1) ) }


        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Harag

          #5
          Re: Math.random syntax

          On 15 Jan 2004 06:11:26 -0800, rbscheer@my-deja.com (Robert Scheer)
          wrote:
          [color=blue]
          >Hi.
          >
          >Reading about the Math.random method I saw that by default it
          >generates between 0 and 1. To generate numbers between a greater range
          >I can use these syntaxes:
          >
          >x = Math.random()/10
          >x = Math.random()*1 0
          >
          >What is the difference between the two? I could not understand the
          >role of the operators here.
          >[/color]

          At the moment I use the following function to get a randum "integer"
          number, I'm new to Javascript so not sure if this is the best way...
          its the way I did it in VBscript.

          function RandomNumber (iMin, iMax) {
          return Math.floor(((iM ax - iMin + 1) * Math.random()) + iMin);
          }


          HTH

          Al.

          Comment

          Working...