[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

Random Numbers

I would like to create random numbers that do not repeat. I have tried using an array like the following:


Dim Used(52) as Integer

Randomize()

redo:
        b = (Int(52 * Rnd()) + 1)

        If Used(b) = b Then
            GoTo redo
        Else
            Used2(b) = b
        End If

This portion of code does not seem to work.

Is there an easier way to create random numbers that do not repeat?
0
joey7643
Asked:
joey7643
1 Solution
 
anaadoulCommented:
Dim MyNums(52) as integer

Private Function DoRandomNumber as integer
Randomize()

redo:
        b = (Int(52 * Rnd()) + 1)
        for i = 0 to 52
           if MyNums(i)=b then goto redo
        next i
       DoRandomNumber=b
end function

Private sub Command1_click()
    MyNums(Pos)=DoRandomNumber
end sub

---------------------------------
There Are 10 Kind OF People
Those Who Understands Binary AND Those Who Don't
0
 
prasitleeCommented:
Dear joey7643,
     Try the following code. This code would give the exact idea about unique random number generating.

Option Explicit

Private m_colUnUsedNumber As Collection

Private Sub Command1_Click()

    Dim i As Integer
    Dim j As Integer
    Dim strExtract As String
    Set m_colUnUsedNumber = New Collection
   
    For i = 0 To 52
        m_colUnUsedNumber.Add i
    Next i
   
    Label1.Caption = ""
    Do Until m_colUnUsedNumber.Count = 0
        j = (Int(m_colUnUsedNumber.Count * Rnd()) + 1)
        strExtract = m_colUnUsedNumber.Item(j)
        Label1.Caption = Label1.Caption & strExtract & " "
        m_colUnUsedNumber.Remove (j)
    Loop
   
End Sub

    I am strongly sure that this code would give you the exact answer.
                                                                             Meng
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now