Solved

Random Numbers

Posted on 2003-12-04
2
617 Views
Last Modified: 2010-05-18
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
Comment
Question by:joey7643
2 Comments
 
LVL 1

Accepted Solution

by:
anaadoul earned 50 total points
ID: 9878270
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
 
LVL 6

Expert Comment

by:prasitlee
ID: 9879048
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
Input past end of file vbs script 9 81
Excel object stays open 19 71
VBA Shell can't Find Word document 11 76
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now