How to display random word in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meko92
    New Member
    • May 2010
    • 1

    How to display random word in an array

    How can I display random three texts in a label by clicking a button

    I want to make a game with 2 players and the second player is the PC. I want the PC to choose one of three word and display it in the label when I click a button.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    dear,
    I hope this will help: see attachment

    br,
    Attached Files

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I see.

      Well place your words into an Array and then generate a Random Number that is between 0 and 2 (within your array boundaries).

      Oh wait a sec,
      This is VB classic isn't it...
      In that case your array starts at 1 (right?)...so you need to pick a random number between 1 and 3 :)


      Here is a function I found while googling that will return a random number between 2 boundaries...I added comments to it to clarify what was happening:
      Code:
      Function Random(Lowerbound  As Long, Upperbound  As Long)
        'Use Randomize to initialize VB's random number generator. 
        'This is called seeding:
        Randomize
      
        'Returning a random number between the upper and lower bounds
        Random = Int(Rnd * Upperbound) + Lowerbound
      End Function
      You can use the random number generated as the index of the element that you want to get.

      So, to summarize you create an array of the word choices your computer player can pick from. You create a method that will return a random number between the array boundaries of word choices. When the computer's turn comes you can retrieve a random word by accessing a word-element at a random index in your array and display that.

      -Frinny

      Comment

      • vb5prgrmr
        Recognized Expert Contributor
        • Oct 2009
        • 305

        #4
        Frinny, you have that backwards. VB6.0 allows for a zero bounded array or even a negative number while VB.NET does not as it sets its base at 1...

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          No, I don't have it backwards.
          I work with VB.NET and the arrays in VB.NET are 0 bound (just like C#).

          -Frinny

          Comment

          Working...