Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random numbers (vb6)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: random numbers (vb6)

    Here is an example for you...

    Dim strColour as String

    Randomize <font color=448800>' Seed the random number generator from the system timer</font color=448800>
    Select Case Int((4 * Rnd) + 1) <font color=448800>' Generate a random number between 1 and 4</font color=448800>
    Case 1
    strColour = "Red"
    Case 2
    strColour = "Yellow"
    Case 3
    strColour = "Green"
    Case 4
    strColour = "Blue"
    End Select

    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: random numbers (vb6)

    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

  4. #4
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: random numbers (vb6)

    thanks guys, these both work perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •