Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Random Numbers

Posted on 2003-12-04
2
Medium Priority
?
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
anaadoul earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

647 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