x
Solved

# Random Numbers

Posted on 2003-12-04
Medium Priority
655 Views
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
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

LVL 1

Accepted Solution

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

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
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

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
Course of the Month15 days, 8 hours left to enroll