Random selection according to the value in other column

ongcaps

New Member
Joined
Sep 7, 2016
Messages
12
Dear Community,
I am trying to create a macro to do what it is so called "random selection proportional to size".

  • I have a table named "source" with names of cities in column B, population in each city in column C and the cumulative population for each row in column D.
  • I have a cell (F5) where the user must enter the number of cities to select
  • I have another table called "result" where the cities selected must be pasted
Randomly select proportional to the size means that cities with more population would have more probability to be selected. To do this there are several steps:

  1. Add the population in all cities (last cell in column D)
  2. Divide the total population by the number of cities to select (F5).
  3. Randomly select one city from the table (this will be the first city and the name must be pasted in the "results" table)
  4. Add the result of step 2 to the cumulative population corresponding to this city (the value in column D for the same row)
  5. Search the result of step 4 in column D until you find a row where the cumulative number that is higher than the result in step 4
  6. The city corresponding to this value (in the same row) is the second city and its name must be pasted in table "results" below the first city selected
  7. This must be repeated until we have the number of cities in cell F5
  8. If the result of step 4 is higher than the total population, then substract the total population (15130) and continue the process from the beginning
An example: Select 3 cities from the following table. Total population=15130. Result of step 2=5043


<tbody>
[TD="align: center"] City [/TD]
[TD="align: center"] Population (.000) [/TD]
[TD="align: center"] Cummulative population [/TD]
[TD="align: center"] Selected [/TD]

[TD="align: center"]London[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"] 2.000[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Madrid[/TD]
[TD="align: center"] 1.500[/TD]
[TD="align: center"] 3.500[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Paris[/TD]
[TD="align: center"] 1.800[/TD]
[TD="align: center"] 5.300[/TD]
[TD="align: center"]1st[/TD]

[TD="align: center"]Prague[/TD]
[TD="align: center"] 900[/TD]
[TD="align: center"] 6.200[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Lisbon[/TD]
[TD="align: center"] 600[/TD]
[TD="align: center"] 6.800[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Zurich[/TD]
[TD="align: center"] 950[/TD]
[TD="align: center"] 7.750[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Bern[/TD]
[TD="align: center"] 350[/TD]
[TD="align: center"] 8.100[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Barcelona[/TD]
[TD="align: center"] 1.300[/TD]
[TD="align: center"] 9.400[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Berlin[/TD]
[TD="align: center"] 1.200[/TD]
[TD="align: center"] 10.600[/TD]
[TD="align: center"]2nd[/TD]

[TD="align: center"]Brujas[/TD]
[TD="align: center"] 430[/TD]
[TD="align: center"] 11.030[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Toledo[/TD]
[TD="align: center"] 150[/TD]
[TD="align: center"] 11.180[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Dublin[/TD]
[TD="align: center"] 620[/TD]
[TD="align: center"] 11.800[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Liverpool[/TD]
[TD="align: center"] 1.700[/TD]
[TD="align: center"] 13.500[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Amsterdam[/TD]
[TD="align: center"] 1.100[/TD]
[TD="align: center"] 14.600[/TD]
[TD="align: center"][/TD]

[TD="align: center"]Oslo[/TD]
[TD="align: center"] 530[/TD]
[TD="align: center"] 15.130[/TD]
[TD="align: center"]3rd[/TD]

</tbody>


hope I have explained properly.
Thanks in advance!
Daniel


Edit:
I forgot to show the result

[TABLE="class: cms_table"]
<tbody>[TR]
[TD]Cities Selected[/TD]
[/TR]
[TR]
[TD]Paris[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[/TR]
[TR]
[TD]Oslo
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Are you specifically looking for a macro, or will a formula solution work?

ABCDEFGH
Oslo
Barcelona
Bern
London

<colgroup><col style="******* 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]City[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Population (.000)[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Cummulative population[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Selected[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]London[/TD]
[TD="bgcolor: #FAFAFA, align: center"]2[/TD]
[TD="bgcolor: #FAFAFA, align: center"]0[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Madrid[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"]2[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Paris[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"]3.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Prague[/TD]
[TD="bgcolor: #FAFAFA, align: center"]900[/TD]
[TD="bgcolor: #FAFAFA, align: center"]5.3[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]14.947[/TD]

[TD="align: center"]6[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Lisbon[/TD]
[TD="bgcolor: #FAFAFA, align: center"]600[/TD]
[TD="bgcolor: #FAFAFA, align: center"]6.2[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]9.065[/TD]

[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Zurich[/TD]
[TD="bgcolor: #FAFAFA, align: center"]950[/TD]
[TD="bgcolor: #FAFAFA, align: center"]6.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]7.963[/TD]

[TD="align: center"]8[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Bern[/TD]
[TD="bgcolor: #FAFAFA, align: center"]350[/TD]
[TD="bgcolor: #FAFAFA, align: center"]7.75[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]0.984[/TD]

[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Barcelona[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.3[/TD]
[TD="bgcolor: #FAFAFA, align: center"]8.1[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]10[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Berlin[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.2[/TD]
[TD="bgcolor: #FAFAFA, align: center"]9.4[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]11[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Brujas[/TD]
[TD="bgcolor: #FAFAFA, align: center"]430[/TD]
[TD="bgcolor: #FAFAFA, align: center"]10.6[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]12[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Toledo[/TD]
[TD="bgcolor: #FAFAFA, align: center"]150[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.03[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Dublin[/TD]
[TD="bgcolor: #FAFAFA, align: center"]620[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.18[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]14[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Liverpool[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.7[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]15[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Amsterdam[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.1[/TD]
[TD="bgcolor: #FAFAFA, align: center"]13.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]16[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Oslo[/TD]
[TD="bgcolor: #FAFAFA, align: center"]530[/TD]
[TD="bgcolor: #FAFAFA, align: center"]14.6[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]17[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]15.13[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet2

[TABLE="******* 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="******* 100%"]
<thead>[TR="bgcolor: #DAE7F5"]
[TH="******* 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="******* 10px, bgcolor: #DAE7F5"]D3[/TH]
[TD="align: left"]=D2+IF(C2>=50,C2/1000,C2)[/TD]
[/TR]
[TR]
[TH="******* 10px, bgcolor: #DAE7F5"]G5[/TH]
[TD="align: left"]=IF(ROW(G5)-ROW($F$5)<$F$5,RANDBETWEEN(1,$D$17*1000)/1000,"")[/TD]
[/TR]
[TR]
[TH="******* 10px, bgcolor: #DAE7F5"]H5[/TH]
[TD="align: left"]=IF(G5="","",INDEX($B$2:$B$16,MATCH(G5,$D$2:$D$16)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



I changed the cumulative population down a row to facilitate the formulas. I used a formula in D3, and copied down the column. The number of cities you want is in F5. Put the formulas in G5 and H5, then drag down the column as far as needed.

The G formula finds a random number between 1 and the total in D17, so the bigger cities get a proportionately larger chance of being picked, and the H formula finds the city that corresponds to that number. The big drawback is that this can choose duplicate cities. You can just recalculate the sheet (F9) until you get a set of unique cities. There are formula ways to avoid duplicates, but they are much more complicated. Before I try that, I'd like to find out if you have a specific preference. I can create a macro if you prefer.

Let me know.
 
Upvote 0
Using your original layout:

ABCDEFG
Berlin
Barcelona
Prague

<colgroup><col style="******* 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]City[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Population (.000)[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Cumulative population[/TD]
[TD="bgcolor: #FAFAFA, align: center"]Selected[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]London[/TD]
[TD="bgcolor: #FAFAFA, align: center"]2[/TD]
[TD="bgcolor: #FAFAFA, align: center"]2[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Madrid[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"]3.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Paris[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"]5.3[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Prague[/TD]
[TD="bgcolor: #FAFAFA, align: center"]900[/TD]
[TD="bgcolor: #FAFAFA, align: center"]6.2[/TD]
[TD="bgcolor: #FAFAFA, align: center"]3[/TD]
[TD="align: right"]3[/TD]

[TD="align: center"]6[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Lisbon[/TD]
[TD="bgcolor: #FAFAFA, align: center"]600[/TD]
[TD="bgcolor: #FAFAFA, align: center"]6.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Zurich[/TD]
[TD="bgcolor: #FAFAFA, align: center"]950[/TD]
[TD="bgcolor: #FAFAFA, align: center"]7.75[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Bern[/TD]
[TD="bgcolor: #FAFAFA, align: center"]350[/TD]
[TD="bgcolor: #FAFAFA, align: center"]8.1[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Barcelona[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.3[/TD]
[TD="bgcolor: #FAFAFA, align: center"]9.4[/TD]
[TD="bgcolor: #FAFAFA, align: center"]2[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]10[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Berlin[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.2[/TD]
[TD="bgcolor: #FAFAFA, align: center"]10.6[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]11[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Brujas[/TD]
[TD="bgcolor: #FAFAFA, align: center"]430[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.03[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]12[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Toledo[/TD]
[TD="bgcolor: #FAFAFA, align: center"]150[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.18[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Dublin[/TD]
[TD="bgcolor: #FAFAFA, align: center"]620[/TD]
[TD="bgcolor: #FAFAFA, align: center"]11.8[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]14[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Liverpool[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.7[/TD]
[TD="bgcolor: #FAFAFA, align: center"]13.5[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]15[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Amsterdam[/TD]
[TD="bgcolor: #FAFAFA, align: center"]1.1[/TD]
[TD="bgcolor: #FAFAFA, align: center"]14.6[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]16[/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FAFAFA, align: center"]Oslo[/TD]
[TD="bgcolor: #FAFAFA, align: center"]530[/TD]
[TD="bgcolor: #FAFAFA, align: center"]15.13[/TD]
[TD="bgcolor: #FAFAFA, align: center"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet3

[TABLE="******* 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="******* 100%"]
<thead>[TR="bgcolor: #DAE7F5"]
[TH="******* 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="******* 10px, bgcolor: #DAE7F5"]E2[/TH]
[TD="align: left"]=IFERROR(MATCH(B2,OFFSET($G$5,0,0,$F$5),0),"")[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



and adding the formula in E2 copied down for the rank, here's a macro that will randomly select your cities proportionately without duplicates:

Rich (BB code):
Sub GetCities()
Dim c As Long, pop As Long, cities(100, 2) As Variant, r As Long, x As Long, MyCount As Long, r1 As Long


    c = Cells(Rows.Count, "B").End(xlUp).Row
    For r = 2 To c
        cities(r, 1) = Cells(r, "B")
        x = IIf(Cells(r, "C") < 50, Cells(r, "C") * 1000, Cells(r, "C"))
        pop = pop + x
        cities(r, 2) = x
    Next r
    
    Range("G:G").ClearContents
    r1 = 5
    For i = 1 To Range("F5")
        y = Int(Rnd() * pop)
        subtot = 0
        For r = 2 To c
            subtot = subtot + cities(r, 2)
            If y < subtot Then
                Cells(r1, "G") = cities(r, 1)
                pop = pop - cities(r, 2)
                cities(r, 1) = cities(c, 1)
                cities(r, 2) = cities(c, 2)
                c = c - 1
                r1 = r1 + 1
                Exit For
            End If
        Next r
    Next i
    
End Sub

Result list starts in G5, but you can change that in the macro by changing the parts in red.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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