Results 1 to 7 of 7

Thread: random function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Location
    Far Far Away
    Posts
    28

    Question random function

    hi all guys im new lol
    anyways
    i am using the Random Function "RND" to randomly choose a string and display it in a label when i click a command button. Spent an hour trying to figure it out but I couldn't.
    Here is the code:

    VB
    ------------------------------------------------------------------------

    lblMonstName.Caption= strMonster (Int(Rnd * UBound(strMonster)))


    Now i think thats the code
    How ever it is not working.
    Anyone understand what i am trying to do.
    Plz someone help lol
    Its driving me nuts
    ..::Knoyali::..
    Life is a game

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    What error (if any) are you getting?
    How is it 'not working' ?
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Location
    Far Far Away
    Posts
    28

    Question

    erm well it simply wont accept the code,
    if u test the code by making your own text box and stuff and label, and then try the code you will see,
    i also have to put "Randomize" in my form load somewhere
    its just that line of code that doesnt work and im really confused
    ..::Knoyali::..
    Life is a game

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    What is strMonster defined as?

    This code works:

    VB Code:
    1. Dim strMonster(5) As String
    2.  
    3. strMonster(1) = "A"
    4. strMonster(2) = "B"
    5. strMonster(3) = "C"
    6. strMonster(4) = "D"
    7. strMonster(5) = "E"
    8.  
    9. Label1.Caption = strMonster(Int(Rnd * UBound(strMonster)) + 1)

    ( you need the +1 because a RND * x will always return numbers between 0 and x-1 )
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Location
    Far Far Away
    Posts
    28

    Talking

    it is dimmed as a string.
    do you know what i am trying to do lol
    coz im not good at explain

    basically a label will randomly choose a strMonster
    and display it wen a command is clicked hope that helps you
    ..::Knoyali::..
    Life is a game

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Location
    Far Far Away
    Posts
    28
    here we go i went home and got the code for it

    Private Sub cmdWilderness_Click()
    lblWildyText.Visible = True
    lblMonstName.Caption = strMonster(Int(Rnd * UBound(strMonster)))
    End Sub

    Private Sub Form_Load()

    Randomize

    Dim strMonster() As String
    ReDim strMonster(4) As String

    strMonster(0) = "Goblin"
    strMonster(1) = "Orc"
    strMonster(2) = "Half Orc"
    strMonster(3) = "Baby Arachnid"
    End Sub

    now see if you can see what the problem is
    ..::Knoyali::..
    Life is a game

  7. #7
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    It's because you have DIMed the String in the Forms Load event - as soon as your code leaves the Load event the variable strMonster is lost, so when you get into the button's click event it doesn't know what strMonster is.. try this instead:

    VB Code:
    1. 'put this line in the General Declarations section of the form
    2. Dim strMonster(4) As String
    3.  
    4. Private Sub Form_Load()
    5. Randomize
    6. strMonster(0) = "Goblin"
    7. strMonster(1) = "Orc"
    8. strMonster(2) = "Half Orc"
    9. strMonster(3) = "Baby Arachnid"
    10. End Sub
    11.  
    12. Private Sub cmdWilderness_Click()
    13. lblWildyText.Visible = True
    14. lblMonstName.Caption = strMonster(Int(Rnd * UBound(strMonster)))
    15. End Sub
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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