How to pick random number without repeating the same number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suprim
    New Member
    • May 2010
    • 8

    How to pick random number without repeating the same number

    I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out?

    For example, if number 10 is call out and display in the label. How can i make sure that number 10 wont be call out and display again?
    Last edited by Niheel; May 10 '10, 12:44 AM. Reason: punctuation, grammar
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    Use a collection with both key and text being a number from 1 to 50. Use Rnd with collectionname. count + 1 to select one of those numbers and retrieve the text. Then remove that item from the collection and thus it will never be called again... as an example
    Code:
    Dim C As New Collection, I As Integer
    For I = 1 To 50
      C.Add CStr(I), CStr(I)
    Next I
    I = Int(Rnd * C.Count + 1)
    MsgBox C.Item(I)
    C.Remove (I)
    MsgBox C.Item(I)
    as you can see from the second message box, a different number appears because the original number has been removed. Don't forget, use the collectionname. count to create your random number so you don't go out of bounds and don't forget to add a check to see if the collection is empty (Count = 0) and if it is to reload the collection with new numbers...

    Edit: Also: Use the Randomize statement in form load so it is called only once...

    Good Luck

    Comment

    • Suprim
      New Member
      • May 2010
      • 8

      #3
      sorry the mentioned doesnot sloved my problem..

      Sorry, i try that but the numbers is repeating times and again...it really doesn't work but it works for choosing the random numbers only...if any other code are there then please post immediately as soon as possible...but thank you for the above code...

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        Hi,

        I did this in Access VBA (using vb5prgrmr's concept) and it works nicely. It should work essentially the same if you are using Visual Studio or some other environment.

        Code:
        Public C As New Collection
        
        Private Sub Form_Open(Cancel As Integer)
        
        For j = 1 To 50
          C.Add CStr(j), CStr(j)
        Next j
        
        Randomize
        
        lstRandoms.RowSourceType = "Value List"
        lstRandoms.RowSource = ""
        
        End Sub
        
        Private Sub Form_Timer()
        
        Dim k As Integer
        
        If C.Count >= 1 Then
            k = Int(Rnd * C.Count + 1)
            lstRandoms.AddItem C(k)
            C.Remove (k)
        End If
        
        End Sub

        Here lstRandoms is the name of my list box control. I set my timer interval at 1000 (1 sec) so that I was not waiting around forever for the sequence to complete, but you might need to run it slower.

        Pat
        Last edited by patjones; May 8 '10, 08:11 PM. Reason: Clarification of explanation...

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          dear,


          I hope this will help (see attachment)
          It places the already Randomized numbers in an array and checks the new random with the values in the array.
          The list is only for visual checking !
          I have only RND from 1 to 10 for fast testing !!

          br,

          Comment

          • Guido Geurs
            Recognized Expert Contributor
            • Oct 2009
            • 767

            #6
            Sorry, my mistake.
            Here it is:
            Attached Files

            Comment

            • Rodney Roe
              New Member
              • Oct 2010
              • 61

              #7
              vb5prgmr your snippet of code is awsome! I was previously doing the same thing with arrays but this cut my code in half. I havn't played with collections very much so thanks for your insight.

              Comment

              • CyberSoftHari
                Recognized Expert Contributor
                • Sep 2007
                • 488

                #8
                @suprim: This is not a place to do your homework. You have to try then post your question. Mod. should close this thread (almost done).

                Comment

                Working...