1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

I need to pick 5 random numbers from a list

Discussion in 'Business Applications' started by budward, Jul 12, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. budward

    budward Thread Starter

    Joined:
    Oct 27, 2006
    Messages:
    75
    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
     

    Attached Files:

  2. Jimmy the Hand

    Jimmy the Hand

    Joined:
    Jul 28, 2006
    Messages:
    1,223
    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
     
  3. budward

    budward Thread Starter

    Joined:
    Oct 27, 2006
    Messages:
    75
    Thanks Jimmy,
    You were right about B4 instead of B1.
    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
     
  4. Jimmy the Hand

    Jimmy the Hand

    Joined:
    Jul 28, 2006
    Messages:
    1,223
    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
     
  5. budward

    budward Thread Starter

    Joined:
    Oct 27, 2006
    Messages:
    75
    Thanks once again,Jimmy
    Bud
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/595113

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice