|
-
Nov 19th, 2008, 03:42 PM
#1
Thread Starter
Member
Producing a random number from a range
I'm trying to make a random number from -100 to 100 in VB6, but all that's coming out is negative numbers. This is what I have:
Code:
Randomize
x = int(rnd * 100) - 100
Any ideas?
-
Nov 19th, 2008, 03:50 PM
#2
Re: Producing a random number from a range
The Rnd function returns a number from 0 to just under 1, so int(rnd * 100) will give you a number from 0 to 99, and subtracting 100 from that will give you -100 to -1
You should be changing *100 to something else.
-
Nov 19th, 2008, 03:53 PM
#3
Thread Starter
Member
Re: Producing a random number from a range
Ah, so would this be it?
Code:
int(rnd * 200) - 100
-
Nov 19th, 2008, 03:57 PM
#4
Re: Producing a random number from a range
That will return (0 to 199) - 100 , which is -100 to 99
-
Nov 19th, 2008, 05:21 PM
#5
Re: Producing a random number from a range
 Originally Posted by MountainDew7
Ah, so would this be it?
Code:
int(rnd * 200) - 100
Not quite, but this does it:
Code:
Int(Rnd * 201) - 100
That will include random numbers ranging from -100 to +100 inclusive.
-
Nov 19th, 2008, 05:32 PM
#6
Re: Producing a random number from a range
The formula is: x = Int(Rnd * (Hi - Lo + 1)) + Lo
Here you have: Lo = -100, Hi = 100
So: x = Int(Rnd * (100 - (-100) + 1)) + (-100)
or: x = Int(Rnd * 201) - 100
-
Nov 20th, 2008, 07:45 PM
#7
New Member
Re: Producing a random number from a range
what if u just wanted a small number like 1 to 6?
-
Nov 20th, 2008, 07:46 PM
#8
New Member
Re: Producing a random number from a range
it would just be Int(Rnd * 7)
-
Nov 20th, 2008, 08:01 PM
#9
Re: Producing a random number from a range
 Originally Posted by hendersondayton
it would just be Int(Rnd * 7)
No. Please read AnHn's post showing the formula. For a range from 1 to 6 inclusive, Lo = 1, Hi = 6. Got it?
-
Nov 20th, 2008, 08:02 PM
#10
Re: Producing a random number from a range
 Originally Posted by hendersondayton
what if u just wanted a small number like 1 to 6?
it would just be Int(Rnd * 7)
Int(Rnd * 7) give you a random integer between 0 to 6 inclusive.
To get a random number between 1 and 6, use the formula in my post#6 above:
x = Int(Rnd * (6 - 1) + 1) +1
or
x = Int(Rnd * 6) + 1
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
|