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:
Any ideas? :confused:Code:Randomize
x = int(rnd * 100) - 100
Printable View
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:
Any ideas? :confused:Code:Randomize
x = int(rnd * 100) - 100
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. ;)
Ah, so would this be it?
Code:int(rnd * 200) - 100
That will return (0 to 199) - 100 , which is -100 to 99
Not quite, but this does it:Quote:
Originally Posted by MountainDew7
That will include random numbers ranging from -100 to +100 inclusive.Code:Int(Rnd * 201) - 100
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
what if u just wanted a small number like 1 to 6?
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?Quote:
Originally Posted by hendersondayton
Int(Rnd * 7) give you a random integer between 0 to 6 inclusive.Quote:
Originally Posted by hendersondayton
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