[RESOLVED] Help with Role Playing Game on VB
Ok so im making an old style RPG on VB6. I cant seem to get my randomize code to work.
heres what I have
Option Explicit
Dim intBHP As Integer
Dim intBossHP As Integer
Dim random As Integer
Private Sub cmdAtk_Click()
cmdBossAtk.Visible = True
cmdPots.Visible = False
End Sub
Private Sub cmdBossAtk_Click()
Randomize
random = Int(Rnd + 2)
random = intBHP
If intBHP < 6 Then
intBHP = 6
End If
If intBHP > 3 Then
intBHP = 3
End If
intBossHP = intBossHP - intBHP
txtBossHP.Text = intBossHP
End Sub
Private Sub cmdItem_Click()
cmdPots.Visible = True
cmdBossAtk.Visible = False
End Sub
Private Sub Form_Load()
intBossHP = 500
End Sub
I want the number to be random but it always picks the same number
Re: Help with Role Playing Game on VB
Randomize always returns the same seed but
Randomize Timer doesn't
Re: Help with Role Playing Game on VB
Some observations
Code:
random = Int(Rnd + 2)
The Rnd Function returns a number equal to or greater that zero and less then one. Therefore the result is always going to be 2
(Int(0 + 2) = 2, and Int(0.99999999 + 2) = 2)
You then overwrite the 'random' number with zero ! You've got the assignment the wrong way round, should be
Also, you should only seed the Random Number Generator once, I suggest you move the Randomize statement into the Form_Load event.
To generate a Random Integer number between two values use:
Code:
random =Int (((Upperbound - Lowerbound +1)*Rnd ) + Lowerbound)
eg to create a Random Number between 1 and 6
Code:
random = Int(((6-1+1)*Rnd)+1)
' Or simplifying it:
random = Int(6*Rnd)+1
Int(6 * Rnd) will generate an Integer number between 0 and 5, adding 1 will give a number between 1 and 6
Re: Help with Role Playing Game on VB
Quote:
Originally Posted by
Doogle
Some observations
Code:
random = Int(Rnd + 2)
The Rnd Function returns a number equal to or greater that zero and less then one. Therefore the result is always going to be 2
(Int(0 + 2) = 2, and Int(0.99999999 + 2) = 2)
You then overwrite the 'random' number with zero ! You've got the assignment the wrong way round, should be
Also, you should only seed the Random Number Generator once, I suggest you move the Randomize statement into the Form_Load event.
To generate a Random Integer number between two values use:
Code:
random =Int (((Upperbound - Lowerbound +1)*Rnd ) + Lowerbound)
eg to create a Random Number between 1 and 6
Code:
random = Int(((6-1+1)*Rnd)+1)
' Or simplifying it:
random = Int(6*Rnd)+1
Int(6 * Rnd) will generate an Integer number between 0 and 5, adding 1 will give a number between 1 and 6
Looks like its working! only problem is that it goes under 3 but not over 6. thanks for the help!
Re: Help with Role Playing Game on VB
Looks like its working! only problem is that it goes under 3 but not over 6.
Sounds like what Doogle said:
Int(6 * Rnd) will generate an Integer number between 0 and 5, adding 1 will give a number between 1 and 6
So why is that a problem? Can you not just ignore numbers less than 3?
Code:
If intBHP < 6 Then
intBHP = 6
End If
If intBHP > 3 Then
intBHP = 3
If the number is 1, 2, 3, 4, 5 you make intBHP = 6
Then immediately following that you say if the number is 4, 5, 6, etc then make it = 3
First time through intBHP = 5 so you make it equal 6 then because it's greater than 3 you you make it 3. So, 5 becomes 6 just to become 3.
Is this what you want?
Re: Help with Role Playing Game on VB
Also, you should only seed the Random Number Generator once, I suggest you move the Randomize statement into the Form_Load event.
Why is that?
Re: Help with Role Playing Game on VB
Quote:
Originally Posted by
jmsrickland
Looks like its working! only problem is that it goes under 3 but not over 6.
Sounds like what Doogle said:
Int(6 * Rnd) will generate an Integer number between 0 and 5, adding 1 will give a number between 1 and 6
So why is that a problem? Can you not just ignore numbers less than 3?
Code:
If intBHP < 6 Then
intBHP = 6
End If
If intBHP > 3 Then
intBHP = 3
If the number is 1, 2, 3, 4, 5 you make intBHP = 6
Then immediately following that you say if the number is 4, 5, 6, etc then make it = 3
First time through intBHP = 5 so you make it equal 6 then because it's greater than 3 you you make it 3. So, 5 becomes 6 just to become 3.
Is this what you want?
I'm sorry, I should have explained better. I said it was a problem, but i forgot to say that it didn't matter. I'm sorry for the mistake. thanks anyway!
Re: Help with Role Playing Game on VB
Quote:
Originally Posted by
jmsrickland
Why is that?
I can't remember where I found this, but it's not my work:
"You could put a RANDOMIZE statement before every call to Rnd if you want, but it's not necessary and may be counter productive. Remember, if you don't specify a seed value with Randomize, Rnd will use the previous number as a seed. So you have a pseudo random number as a seed to get the next random number. Whereas if you use Randomize all the time, you are using the current time as a seed. A value that is not nearly so random and is, in fact linear in growth and has a small amount of variability."
Re: Help with Role Playing Game on VB
Alright, Thanks everyone! I got it to work and now its time to move on with the coding.