# I need to pick 5 random numbers from a list

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

#### budward

of 22 numbers. My example explains in more detail what I would like to do.
Thanks in advance for all the help I have received and continue to receive at this forum.
...Bud

#### Attachments

• 15.5 KB Views: 274

#### Jimmy the Hand

Hi Bud,

this macro does the job.
Code:
``````Sub RandomNumbers()
Dim Src As Range, NextSet As Range
Dim i As Long, j As Long, NumberOfSets As Long
Dim n As Long

Set Src = Range("A1:V1")
NumberOfSets = Range("B4")
For i = 1 To NumberOfSets
Set NextSet = Range("D" & 3 + i).Resize(, 5)
Randomize
For j = 1 To 5
n = Int(22 * Rnd(1)) + 1
NextSet(j) = Src(n)
Next j
NextSet.Sort key1:=NextSet(1), Orientation:=xlLeftToRight
Next i
End Sub``````
I assumed you want to put the number of random number sets into B4, instead of B1, as was indicated in the sample workbook. But if it's really B1 then the code must be modified, but I'm sure you can do that easily.

Also, please note that this macro does not check if there are identical number in a set, or if there are identical sets. This was not an explicit requirement, so I left it out for now. If you need something like this, it can be done, of course.

Jimmy

#### budward

Thanks Jimmy,
If I can check for identical numbers in a set and identical sets, that would be awesome!
I am always amazed that no matter what idea I come up with, there is always someone who knows how to make it happen.
thanks again...bud

#### Jimmy the Hand

Bud,
here's the modified macro:

Code:
``````Sub RandomNumbers()
Dim Src As Range, NextSet As Range
Dim i As Long, j As Long, NumberOfSets As Long, n As Long, pos As Long
Dim NextSetString As String, AllSetsString As String, s As String

Randomize
Set Src = Range("A1:V1")
NumberOfSets = Range("B4")
[COLOR="Red"][B]    If NumberOfSets > 3160080 Then
MsgBox "The requested number of sets is greater than the maximum possible."
Exit Sub
End If[/B][/COLOR]
AllSetsString = ""
i = 1
While i < NumberOfSets + 1
[COLOR="SeaGreen"][B]        If i + 3 > Rows.Count Then
MsgBox "There's no more rows on this sheet to place new sets. Exiting."
Exit Sub
End If[/B][/COLOR]
Set NextSet = Range("D" & 3 + i).Resize(, 5)
NextSetString = ""
j = 0
While j < 5
n = Src(Int(22 * Rnd(1)) + 1)
[COLOR="Blue"][B]s = Right(CStr(n + 100), 2)[/B][/COLOR]
pos = InStr(NextSetString, s)
If (pos = 0) Or (pos Mod 2 <> 1) Then
j = j + 1
NextSetString = NextSetString & s
NextSet(j) = n
End If
Wend
NextSet.Sort key1:=NextSet(1), Orientation:=xlLeftToRight
NextSetString = ""
For j = 1 To 5
s = Right(CStr(NextSet(j) + 100000), 2)
NextSetString = NextSetString & s
Next
pos = InStr(AllSetsString, NextSetString)
If (pos = 0) Or (pos Mod 10 <> 1) Then i = i + 1
Wend
End Sub``````
There are 3 imperfections in the code that I know of.
• It works well only as long as all numbers in the list are >0 and <100, integers. (See the blue part of the code.)
• Checking for the maximum possible number of different sets is hardcoded for 22 items inthe list.(See the red part of the code.)
• If the requested number of set is greater than the number of rows on the sheet, the macro stops. (See the green part of the code.)

Jimmy

#### budward

Thanks once again,Jimmy
Bud

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

As Seen On