Solved

Random Numbers

Posted on 2003-12-04
2
605 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

20 Experts available now in Live!

Get 1:1 Help Now