Results 1 to 3 of 3

Thread: [RESOLVED] Math.random()

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    Resolved [RESOLVED] Math.random()

    Hi,
    I want my program to generate random numbers between -20 and +20 so how should I modify the following line ?
    var randNum=Math.floor(Math.random()*???)
    thanks for your help

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Math.random()

    I think all languages actually generate random numbers from 0 to the specified number (or one less than that, whatever). So you can generate a number out of 40, then subtract 20 from it.
    VB Code:
    1. var num = Math.floor(Math.random()*40)-20;
    Show Appreciation. Rate Posts.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Math.random()

    Which generates the closed interval [-20;19], i.e. including those two. If you want to include 20, too, you're generating 41 numbers and must adjust.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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