Unique Random numbers and Excluding old

hkaldis

New Member
Joined
Apr 20, 2015
Messages
5
Hi guys,

sorry if this is already answered somewhere i search alot and no luck finding it...

I have the following issue.


Lets assume i have a list in Column A (E.g. Names) and i would like to pick 3 members (never the same) of that list in the following Cells: B2 - C2- D2


Then in the bellow row (cells: B3 - C3 - D3) i would like again to pick 3 unique members of list with names but EXCLUDING the values that i have now in B2 - C2 - D2


Any suggestions?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi and welcome to the MrExcel Message Board.

You could add another column with random numbers in it e.g. =RAND().

Sort the data by the random number column.
Select the first three names for your first row and the next three names for the second row.

For a new trial just re-sort the data.


Excel 2013
ABCDEF
1NamenName 1Name 2Name 3
2X80.52974X8X1X7
3X10.570586X4X17X3
4X70.012688
5X40.505866
6X170.583634
7X30.251436
8X20.66405
9X50.417563
10X140.901413
11X130.803334
12X110.539853
13X180.155227
14X160.932091
15X150.905596
16X100.958468
17X90.076347
18X190.469093
19X60.877983
20X120.716432
Sheet1
Cell Formulas
RangeFormula
D2=A2
D3=A5
E2=A3
E3=A6
F2=A4
F3=A7
B2=RAND()
B3=RAND()
B4=RAND()
B5=RAND()
B6=RAND()
B7=RAND()
B8=RAND()
B9=RAND()
B10=RAND()
B11=RAND()
B12=RAND()
B13=RAND()
B14=RAND()
B15=RAND()
B16=RAND()
B17=RAND()
B18=RAND()
B19=RAND()
B20=RAND()
 
Upvote 0
Thanks RickXL but my solution should not include sorting. So what i thought is to use the array trick with

=LARGE(ROW($1:$10)*NOT(COUNTIF($D$2:D2, ROW($1:$10))), RANDBETWEEN(1, 11-COLUMN(A1)))

For columns and the equivalent for Rows. My problem is that i am able to create the unique random number in the list and in D E F columns afterwords (so to bring with vlookup the name) but i cannot then exclude the already used...




Hi and welcome to the MrExcel Message Board.

You could add another column with random numbers in it e.g. =RAND().

Sort the data by the random number column.
Select the first three names for your first row and the next three names for the second row.

For a new trial just re-sort the data.

Excel 2013
ABCDEF
1NamenName 1Name 2Name 3
2X80.52974X8X1X7
3X10.570586X4X17X3
4X70.012688
5X40.505866
6X170.583634
7X30.251436
8X20.66405
9X50.417563
10X140.901413
11X130.803334
12X110.539853
13X180.155227
14X160.932091
15X150.905596
16X100.958468
17X90.076347
18X190.469093
19X60.877983
20X120.716432

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
D2=A2
E2=A3
F2=A4
D3=A5
E3=A6
F3=A7
B2=RAND()
B3=RAND()
B4=RAND()
B5=RAND()
B6=RAND()
B7=RAND()
B8=RAND()
B9=RAND()
B10=RAND()
B11=RAND()
B12=RAND()
B13=RAND()
B14=RAND()
B15=RAND()
B16=RAND()
B17=RAND()
B18=RAND()
B19=RAND()
B20=RAND()

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Just got a headache by reading that Grid of Random Integers but it surely works. Thanks XOR LX!

Ha! Are my explanations that bad? :)

Why just why they don't put another option at random formula RANDBETWEEN(MIN,MAX,EXCLUDING) it would be so much helpful instead of trying to understand the arrays...

Yes, perhaps you're right. Complementary operations are not something which Excel does well.

Only the other week someone was looking for a solution to the problem of excluding certain ranges from a larger, union of ranges, i.e. given the range e.g. A1:A10, C1:C20, E1:E30, remove just the cell C13.

Of course, the only way to do this (I think) is to redefine it as A1:A10, C1:C12, C14:C20, E1:E30. But if you've got a lot of single cells that you want excluding (which was the case for the OP), then such a process can be extremely time-consuming and hardly ideal.

Regards
 
Upvote 0
Both your solution and explanation are awesome!

Thanks it a life saver...

out of curiosity, i tried to do the following

In C4 i created the following array:

=LARGE(ROW(INDIRECT($J$4&":"&$K$4))*NOT(COUNTIF($B4:B4, ROW(INDIRECT($J$4&":"&$K$4)))), RANDBETWEEN($J$4, $K$4-COLUMN(A1)+1))

Where in J4 is the minimum and K4 the maximum of randbetween.

This allows me to have unique numbers in columns and works fine. Now if i modify the *NOT part of the formula to:
*NOT(COUNTIF($B4:B4,ROW(INDIRECT($J$4&":"&$K$4)*NOT(COUNTIF($C3:$H3),ROW(INDIRECT($J$4&":"&$K$4)

i think it would work as i am excluding all values. My problem is that when i try it i get a too many arguments warning in excel :(

What you think?

I have it in an excel here:

https://dl.dropboxusercontent.com/u/26018924/unique random.xlsx


Ha! Are my explanations that bad? :)



Yes, perhaps you're right. Complementary operations are not something which Excel does well.

Only the other week someone was looking for a solution to the problem of excluding certain ranges from a larger, union of ranges, i.e. given the range e.g. A1:A10, C1:C20, E1:E30, remove just the cell C13.

Of course, the only way to do this (I think) is to redefine it as A1:A10, C1:C12, C14:C20, E1:E30. But if you've got a lot of single cells that you want excluding (which was the case for the OP), then such a process can be extremely time-consuming and hardly ideal.

Regards
 
Upvote 0
You're effectively attempting to achieve what I did, though using a couple of COUNTIF statements in place of my use of FREQUENCY.

I think you've got a bit confused with your syntax, though, as well as the relative/absolute referencing for your ranges. What's more, if you try to avoid CSE by taking products of arrays like that, you'll find that Boolean FALSE returns end up as numerical zeroes, and so will potentially be returned.

Your formula in C4 would be:

=LARGE(ROW(INDIRECT($J$4&":"&$K$4))*NOT(COUNTIF($B4:B4,ROW(INDIRECT($J$4&":"&$K$4))))*NOT(COUNTIF($C3:$H$3,ROW(INDIRECT($J$4&":"&$K$4)))),RANDBETWEEN($J$4, $K$4-COLUMN(A1)+1))

though, as I said, you will need to convert this to a CSE set-up with some IF statements to be sure of correct returns; otherwise you will most likely find yourself with zeroes in many places.

I leave that conversion to you, if interested.

Regards
 
Upvote 0
Hi,

You could try a macro solution.

This one works just like my original idea except that instead of the list of random numbers being on the worksheet it is in an array or, more accurately, a sorted list. This list contains the random numbers and the occupied row numbers. That is, the one with names in. The list automatically sorts itself into random number order. This means that the first six row numbers can be used to locate the names in a random, non-repeating, sequence.

A couple of For Next loops take the first six names and write them to the worksheet.

The worksheet name can be changed by overtyping it.
Extra names in the list will automatically be included.
More output rows and columns can be added by changing the numbers on the final pair of For Next loops.

Code:
Sub Random_N()

    Dim N As Long, i As Long, iRow As Long, iCol As Long
    Dim mySL As Object
    Dim ws1 As Worksheet
    
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set mySL = CreateObject("System.Collections.SortedList")
    
    With ws1
        ' See how many names there are in column A and set the list size
        N = .Cells(.Rows.Count, "A").End(xlUp).Row
        mySL.Capacity = N - 1
        
        ' Fill array with random numbers and row numbers
        For i = 2 To N
            mySL.Item(Rnd) = i
        Next
        
        ' Write out the random names
        i = 0
        For iRow = 2 To 3       ' Row numbers for output
            For iCol = 2 To 4   ' Column Numbers for output
                .Cells(iRow, iCol).Value = .Cells(mySL.GetByIndex(i), 1).Value
                i = i + 1
            Next
        Next
        
    End With

End Sub
 
Upvote 0
Thanks Rick this is also very helpful

I ll try both as in large tables i think the array will be slow

Hi,

You could try a macro solution.

This one works just like my original idea except that instead of the list of random numbers being on the worksheet it is in an array or, more accurately, a sorted list. This list contains the random numbers and the occupied row numbers. That is, the one with names in. The list automatically sorts itself into random number order. This means that the first six row numbers can be used to locate the names in a random, non-repeating, sequence.

A couple of For Next loops take the first six names and write them to the worksheet.

The worksheet name can be changed by overtyping it.
Extra names in the list will automatically be included.
More output rows and columns can be added by changing the numbers on the final pair of For Next loops.

Code:
Sub Random_N()

    Dim N As Long, i As Long, iRow As Long, iCol As Long
    Dim mySL As Object
    Dim ws1 As Worksheet
    
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set mySL = CreateObject("System.Collections.SortedList")
    
    With ws1
        ' See how many names there are in column A and set the list size
        N = .Cells(.Rows.Count, "A").End(xlUp).Row
        mySL.Capacity = N - 1
        
        ' Fill array with random numbers and row numbers
        For i = 2 To N
            mySL.Item(Rnd) = i
        Next
        
        ' Write out the random names
        i = 0
        For iRow = 2 To 3       ' Row numbers for output
            For iCol = 2 To 4   ' Column Numbers for output
                .Cells(iRow, iCol).Value = .Cells(mySL.GetByIndex(i), 1).Value
                i = i + 1
            Next
        Next
        
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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