Results 1 to 9 of 9

Thread: [RESOLVED] Help with Role Playing Game on VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    4

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Help with Role Playing Game on VB

    Randomize always returns the same seed but

    Randomize Timer doesn't


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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)
    Code:
    random = intBHP
    You then overwrite the 'random' number with zero ! You've got the assignment the wrong way round, should be
    Code:
    intBHP = random
    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
    Last edited by Doogle; Jun 16th, 2011 at 03:29 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    4

    Re: Help with Role Playing Game on VB

    Quote Originally Posted by Doogle View Post
    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)
    Code:
    random = intBHP
    You then overwrite the 'random' number with zero ! You've got the assignment the wrong way round, should be
    Code:
    intBHP = random
    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!

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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?
    Last edited by jmsrickland; Jun 16th, 2011 at 06:22 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    4

    Re: Help with Role Playing Game on VB

    Quote Originally Posted by jmsrickland View Post
    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!

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help with Role Playing Game on VB

    Quote Originally Posted by jmsrickland View Post
    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."
    Last edited by Doogle; Jun 17th, 2011 at 12:37 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    4

    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.

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