Results 1 to 8 of 8

Thread: Random function, do you know??

  1. #1
    Guest

    Angry

    Anyone Know how to random 5 number, and when i click command button 1st time, it has a msgbox with a randomize number(ex:2), then the number(2) is cancel. And i click again, a randomize number(5),then 5 is cancel. That means what i click 5 times command button, there is no number left, Empty. Can You tell me what the code, plz!! Need it Ugently!!

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Can you rephrase that question ?

    - jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    leung, do you mean the number must append once only?

    Code:
    Option Explicit
    Private dict As Dictionary
    
    Private Sub form_load()
        Set dict = New Dictionary
        dict.CompareMode = TextCompare
    End Sub
    
    Private Sub Command1_Click()
    Dim i As Integer
    
    If dict.Count = 5 Then
        dict.RemoveAll
        Msgbox "No More number!"
    Else
    i = GetNum
    Do While Not dict.Exists(i)
        dict.Add i, i
        MsgBox "New Number is " & i
        DoEvents
    Loop
    End Sub
    
    Private Function GetNum()
        Randomize Timer
        GetNum = Int((5 * Rnd) + 1)
    End Function
    Enjoy the sample code.


  4. #4
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Question Dictionary??

    Nice code, but I get an error stating: udt not defined and Dictionary is marked.

    What is Dictionary?
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    onerrorgoto, Disctionary is an VB Script Object and you need to reference to the Microsoft Script Runtime Library (Scrrun.dll) at Project|References

    Cheers!

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Code:
    Private Sub Command1_Click()
    
        Dim intRandom As Integer
        Static intAdded As Integer
        Dim bFound As Boolean
        Dim intDummy As Integer
        Static colRandomNumbers As New Collection
        
        Randomize
        
        If intAdded = 5 Then
            MsgBox "No more numbers available"
            Exit Sub
        End If
        
        Do Until bFound
            intRandom = Int(5 * Rnd + 1)
            On Error Resume Next
            intDummy = colRandomNumbers.Item(CStr(intRandom))
            bFound = (Err = 0)
            If bFound Then
                ' It's already in the collection
                Err.Clear
            Else
                ' It's not, so add it
                colRandomNumbers.Add intRandom, CStr(intRandom)
                bFound = True
                intAdded = intAdded + 1
                MsgBox "Number is " & intRandom
            End If
        Loop
       
    End Sub

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here's another way:
    Code:
    Option Explicit
    Dim intArray(0 To 4) As Integer
    
    Public Sub command1_click()
    
        Static Counter As Integer
        Static Y As Integer
        Dim x As Integer
        Dim I As Integer
        Dim Exists As Boolean
        Randomize Timer
        Counter = Counter + 1
        If Counter > 5 Then MsgBox "Empty": Exit Sub
    
        
    Handle:
        I = Int(Rnd * 5) + 1
        For x = LBound(intArray) To UBound(intArray)
            If I = intArray(x) Then
                Exists = True
                Exit For
            End If
        Next x
        
        If Exists = True Then
            Exists = False
            GoTo Handle:
        End If
        
        intArray(Y) = I
        Y = Y + 1
        
        Select Case I
            Case Is = 1: MsgBox I:  Exit Sub
            Case Is = 2: MsgBox I:  Exit Sub
            Case Is = 3: MsgBox I:  Exit Sub
            Case Is = 4: MsgBox I:  Exit Sub
            Case Is = 5: MsgBox I:  Exit Sub
        End Select

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile

    Martin, never think abt to use the Collection object.
    Thank to remind me abt this object.

    [Edited by Chris on 01-11-2001 at 01:35 AM]

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