Random generate multiple names from a list with exclusions & sorting

camspy

New Member
Joined
Jan 7, 2022
Messages
43
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Here's my table:

Screenshot_1.png


Here's also the sheet:

test.xlsx
ABCDEF
1NamesRankExcludesimplesortedsorted+excluded
2Abigail65Olivia
3Sophia25Mila
4Amelia30
5Elizabeth80Isabella,Sophia,Amelia
6Ava15Gianna,Emma
7Isabella35Luna
8Gianna60
9Harper50
10Mila100Sofia
11Olivia5
12Charlotte20
13Evelyn45Camila,Ava,Abigail
14Avery95Charlotte
15Emily90Evelyn,Mia
16Ella75
17Sofia85Avery,Emily
18Mia40Elizabeth,Ella
19Camila55
20Emma10Harper
21Luna70
Sheet1


I need to generate 10 random names (delimited), without duplicates, in each cell of columns D, E and F, the rules are below:
  1. In column D: just the list of 10 randomly generated and delimited names, without dupes;
  2. In column E: the same as in column D, but the results should be sorted by ranks taken from B2:B21, from high to low;
  3. In column F: the same as in column E, but the results will have per-row exclusions, listed in column C.

Each result should contain 10 names, and visually should look like this: Sophia,Amelia,Isabella,Mia,Evelyn,Harper,Camila,Gianna,Abigail,Luna

I have tried many of the solutions with RANDARRAY and RANDBETWEEN formulas found on the internet, but didn't succeed myself.

Maybe it's not even possible to have it working using just formulas?
Maybe VBA is required?

Any help is appreciated.
Thanks in advance.
 
You're welcome & thanks for the feedback.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You're welcome & thanks for the feedback.
Hi Fluff,
I've tested your formulas with the field data with hundreds of rows.
Particularly I've tested the formula from col F.

I counted the frequency of appearance of the names in the generated column.
The thing is that some names get generated 30+ times across the sheet, while the others - 0 times.

Is there a way of having distribution of the names more smooth/fair?

Perhaps formulas won't be able to help with that and I'm afraid, that VBA may be needed.

I also thought, that logically, maybe the names with higher rank could have some better distribution across the sheet, comparing to names with lower rank.
 
Upvote 0
Whilst I would expect some variation in the number of times the names are pulled, I'm surprised it's that much.
However there is no way (that I know of) to change that as I suspect that all the rand functions work the same way. I also doubt that VBA would be any better.
 
Upvote 0
Whilst I would expect some variation in the number of times the names are pulled, I'm surprised it's that much.
However there is no way (that I know of) to change that as I suspect that all the rand functions work the same way. I also doubt that VBA would be any better.
What if using an additional helper column with some sort of extra data to make the results more steady?
 
Upvote 0
I've no idea how that would work. At the end of the day randarray is random, which is what you wanted.
 
Upvote 0
I've no idea how that would work. At the end of the day randarray is random, which is what you wanted.
Yes sure. I just thought that, randomness will follow the probability theory, meaning that each name should more or less get similar distribution
 
Upvote 0
I would expect the same thing, but the results for col F will be skewed as you are excluding some names on some rows.
 
Upvote 0
In my tests I was excluding just 1 name for each set. So the results won't contain own name.
 
Upvote 0
i made perhaps an error, but i have no 10 exclusive names, max. 8
VBA Code:
Sub list10()
     Dim arr(), Excl(), Result(1 To 10000, 1 To 4), iRes

     a = Sheets("blad1").ListObjects("TBL_Namen").DataBodyRange     'the names
     t = Timer                                                  'start chronometer

     For iRes = 1 To UBound(Result)                             'loop for x good combinations
          s = "": ptr = 1                                       'start with no exclusives and pointer 1
          rand = Application.Transpose(WorksheetFunction.RandArray(UBound(a)))     'as many random numbers as names
          ReDim arr(1 To 10, 1 To 2): ReDim Excl(1 To 10, 1 To 2)     'reset tempory arrays
          For i = 1 To UBound(a)                                'make as many loops as there are names
               r = Application.Match(WorksheetFunction.Small(rand, i), rand, 0)     'row of the i-smallest value for Rand
               If i <= UBound(arr) Then arr(i, 1) = a(r, 1): arr(i, 2) = a(r, 2)     'for the first 2 columns there are no exclusives, so write without checking until enough names found

               m = "": If Len(s) > 0 Then m = Application.Match(a(r, 1), Split(s, ","), 0)
               If Not IsNumeric(m) Then
                    b = True
                    If Len(a(r, 3)) > 0 Then
                         sp = Split(a(r, 3), ",")
                         arr1 = Application.Transpose(Application.Index(arr, 0, 1))
                         For j = 0 To UBound(sp)
                              r1 = Application.Match(sp(j), arr1, 0)
                              If IsNumeric(r1) Then b = False: Exit For
                         Next
                         If b Then
                              Excl(ptr, 1) = a(r, 1): Excl(ptr, 2) = a(r, 2): s = s & "," & a(r, 3) & "," & a(r, 1): ptr = ptr + 1
                         Else
     'MsgBox Join(arr1, ",") & vbLf & a(r, 1) & vbLf & a(r, 3)
                         End If
                         If ptr > UBound(Excl) Then Exit For
                    End If
               End If
          Next
          If Excl(UBound(Excl), 1) = "" Then Excl(UBound(Excl), 1) = "ERROR" & ptr - 1: Excl(UBound(Excl), 2) = -1

          Result(iRes, 1) = Join(Application.Transpose(Application.Index(arr, 0, 1)), ",")
          arr1 = Application.Sort(arr, 2, -1)
          Result(iRes, 2) = Join(Application.Transpose(Application.Index(arr1, 0, 1)), ",")
          Arr2 = Application.Sort(Excl, 2, -1)
          Result(iRes, 3) = Join(Application.Transpose(Application.Index(Arr2, 0, 1)), ",")
          Result(iRes, 4) = s
          DoEvents
     Next

     With Range("E2")
          .Resize(10000, 4).ClearContents
          .Resize(UBound(Result), UBound(Result, 2)).Value = Result
     End With
     MsgBox Timer - t
End Sub
 
Upvote 0
i made perhaps an error, but i have no 10 exclusive names, max. 8
VBA Code:
Sub list10()
     Dim arr(), Excl(), Result(1 To 10000, 1 To 4), iRes

     a = Sheets("blad1").ListObjects("TBL_Namen").DataBodyRange     'the names
     t = Timer                                                  'start chronometer

     For iRes = 1 To UBound(Result)                             'loop for x good combinations
          s = "": ptr = 1                                       'start with no exclusives and pointer 1
          rand = Application.Transpose(WorksheetFunction.RandArray(UBound(a)))     'as many random numbers as names
          ReDim arr(1 To 10, 1 To 2): ReDim Excl(1 To 10, 1 To 2)     'reset tempory arrays
          For i = 1 To UBound(a)                                'make as many loops as there are names
               r = Application.Match(WorksheetFunction.Small(rand, i), rand, 0)     'row of the i-smallest value for Rand
               If i <= UBound(arr) Then arr(i, 1) = a(r, 1): arr(i, 2) = a(r, 2)     'for the first 2 columns there are no exclusives, so write without checking until enough names found

               m = "": If Len(s) > 0 Then m = Application.Match(a(r, 1), Split(s, ","), 0)
               If Not IsNumeric(m) Then
                    b = True
                    If Len(a(r, 3)) > 0 Then
                         sp = Split(a(r, 3), ",")
                         arr1 = Application.Transpose(Application.Index(arr, 0, 1))
                         For j = 0 To UBound(sp)
                              r1 = Application.Match(sp(j), arr1, 0)
                              If IsNumeric(r1) Then b = False: Exit For
                         Next
                         If b Then
                              Excl(ptr, 1) = a(r, 1): Excl(ptr, 2) = a(r, 2): s = s & "," & a(r, 3) & "," & a(r, 1): ptr = ptr + 1
                         Else
     'MsgBox Join(arr1, ",") & vbLf & a(r, 1) & vbLf & a(r, 3)
                         End If
                         If ptr > UBound(Excl) Then Exit For
                    End If
               End If
          Next
          If Excl(UBound(Excl), 1) = "" Then Excl(UBound(Excl), 1) = "ERROR" & ptr - 1: Excl(UBound(Excl), 2) = -1

          Result(iRes, 1) = Join(Application.Transpose(Application.Index(arr, 0, 1)), ",")
          arr1 = Application.Sort(arr, 2, -1)
          Result(iRes, 2) = Join(Application.Transpose(Application.Index(arr1, 0, 1)), ",")
          Arr2 = Application.Sort(Excl, 2, -1)
          Result(iRes, 3) = Join(Application.Transpose(Application.Index(Arr2, 0, 1)), ",")
          Result(iRes, 4) = s
          DoEvents
     Next

     With Range("E2")
          .Resize(10000, 4).ClearContents
          .Resize(UBound(Result), UBound(Result, 2)).Value = Result
     End With
     MsgBox Timer - t
End Sub
Hi Bsalv, thank you for your help.
Could you explain how to run it and what results will it bring?
Is your code related to my later post? Random generate multiple names from a list with exclusions & sorting
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top