Results 1 to 4 of 4

Thread: Counter to count # of loops

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    1

    Counter to count # of loops

    Ok, so I basically have 3 labels, that each generate a unique random number between 1 to 5, when I click on the command box. I need the the 4th label to show the number of loops it took to get the 3 unique numbers. I'm stuck.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Counter to count # of loops

    Loops? How are you determining random nubers in a loop?

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Counter to count # of loops

    baja vu's question aside, put a counter in the loop, increment it by one.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Counter to count # of loops

    Welcome to the forums...

    Here's an example:
    Code:
    Option Explicit
    
    Dim num(2) As Integer
    
    '~~~ Function for generating Random Number within a range
    Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer
    
      RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
    
    End Function
    
    Private Sub Command1_Click()
      Randomize
      
      Dim i           As Integer
      Dim Temp        As Integer
      Dim intCounter  As Integer
      
      For i = 0 To 2                        '~~~ Initial value will be 0
        num(i) = 0
      Next i
      
      For i = 0 To 2
        Do
          Temp = RandomInteger(1, 5)        '~~~ Random number generation
          intCounter = intCounter + 1       '~~~ Counter
        Loop Until UniqueNum(Temp) = True   '~~~ Check if it's a unique number
        
        num(i) = Temp                       '~~~ Saving the number
      Next i
      
      '~~~ Finally displaying it in the LabelBoxes
      Label1.Caption = num(0)
      Label2.Caption = num(1)
      Label3.Caption = num(2)
      
      MsgBox intCounter     '~~~~ Display the number of loops
    End Sub
    
    '~~~ Function to check whether it's unique or not
    Private Function UniqueNum(ByVal number As Integer) As Boolean
      Dim i As Integer
      
      For i = 0 To 2
        If num(i) = number Then
          UniqueNum = False
          Exit Function
        End If
      Next i
      
      UniqueNum = True
    End Function
    .....

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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