VB.NET: How do I randomize Words?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crossfade52
    New Member
    • Nov 2006
    • 1

    #1

    VB.NET: How do I randomize Words?

    Hi, I'm new to VB.net. For a school project, I am trying to assign five variables to one label. And I want one of those variables to randomly show up when I hit the go button. Right now, a snip of my code looks like this:

    Dim StrJob1 As String = "Teacher"
    Dim StrJob2 As String = "House Maid"
    Dim StrJob3 As String = "Lumber Jack"
    Dim StrJob4 As String = "Doctor"
    Dim StrJob5 As String = "Lawyer"
    Dim StrJob6 As String = "Chef"



    I hope this was clear... thanks in advanced.
  • peterggmss
    New Member
    • Oct 2006
    • 6

    #2
    Use a random integer

    Code:
    Dim RandomWordNumber as Integer
    
    Private Sub cmdShowWord_Click()
    
         'Generate a random number
         RandomWordNumber = Int(6 * Rnd + 1)
    
         'See which number is for which word and change the label
         If RandomWordNumber = 1 Then
              lblWord.Caption = "Teacher"
         End If
    Start with that and see what you can do, this code is copyright 2006 Peter Browne, GPL v2 Licensed - feel free to use but you must give credit as "Code Portion(s) (c) 2006 Peter Browne, peterggmss (thescripts.com ), peterrbrowne@ho tmail.com, GPL v2, Used Under License"

    Comment

    • Seith
      New Member
      • Oct 2006
      • 16

      #3
      Code:
      'Required to tell the script to make a random number
      Public Function Rand(ByVal Low As Long, ByVal High As Long) As Long
        Rand = Int((High - Low + 1) * Rnd) + Low
      End Function
      
      Private Sub Command1_Click()
      
      Dim random_number As Integer
      Randomize 'Declair Random
      random_number = Rand(1, 6) 'Generates random number from (Lowest, Highest)
      
      Select Case random_number 'Sets the value that will change in the Cases
          Case Is = 1
              Label1.Caption = "Job One"
          Case Is = 2
              Label1.Caption = "Job Two"
          Case Is = 3
              Label1.Caption = "Job Three"
          Case Is = 4
              Label1.Caption = "Job Four"
          Case Is = 5
              Label1.Caption = "Job Five"
          Case Is = 6
              Label1.Caption = "Job Five"
      End Select
      
      End Sub
      Requirements:
      Command Button "command1"
      Label "label1"

      Action:
      Clicking on command1 will make a random number from 1 to 6 and depending on the result (number) changes label1's caption to a specified (job title).

      Customize:

      Change the names of command1 and label1 through out the script
      Change what the specified (job titles) are

      Information:
      Case = 1 is like saying "If {variable} = 1 then" it is used when a lot of different outcomes are possible, and it is how you will be taught to use them in the near future in your class ;)


      P.S. You can't copyright common code that's used in everyday use of Visual Basic, only something you have created... if you attempted to patent this it would fail under being "not unique". Just so you know...

      Comment

      Working...