• random numbers (vb6)

    • This topic has 3 replies, 3 voices, and was last updated 22 years ago.
    Author
    Topic
    #387683

    Hello all

    I need to know how to code the random number generator properly. I think you have to use the SEED command or something. What I really want to do is have VB randomly generate one of 4 numbers for me, 5, 11, 32 or 46 but have no clue how to do this. Also is it possible to have VB Generate one of 4 random words like Red, green, ect.

    Thanks everyone

    Viewing 1 reply thread
    Author
    Replies
    • #677475

      Here is an example for you…

      Dim strColour as String

      Randomize ‘ Seed the random number generator from the system timer
      Select Case Int((4 * Rnd) + 1) ‘ Generate a random number between 1 and 4
      Case 1
      strColour = “Red”
      Case 2
      strColour = “Yellow”
      Case 3
      strColour = “Green”
      Case 4
      strColour = “Blue”
      End Select

      StuartR

    • #677476

      Put an instruction Randomize at the beginning of your code to initialize the random number generator. To select one of four specific numbers or strings, put the items in an array, for example:

      Dim strarrColors(3) As String
      strarrColors(0) = “Red”
      strarrColors(1) = “Green”
      strarrColors(2) = “Blue”
      strarrColors(3) = “Yellow”

      The code to select a random item is:

      Dim intIndex As Integer
      Dim strItem As String
      intIndex = Int(Rnd * 4) ‘ random value between 0 and 3
      strItem = strarrColors(intIndex) ‘ random item

    Viewing 1 reply thread
    Reply To: random numbers (vb6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: