|
-
Nov 6th, 2006, 11:47 AM
#1
Thread Starter
Fanatic Member
[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
-
Nov 6th, 2006, 12:16 PM
#2
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:
var num = Math.floor(Math.random()*40)-20;
-
Nov 6th, 2006, 12:27 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|