Selecting 7 numbers from a weighted list of numbers

AndyStanton

New Member
Joined
Mar 6, 2018
Messages
4
Hi All,

I'm trying to generate a list of 7 random numbers from a list of 45 numbers which each have a previous frequency of selection. I'm trying to weight the random numbers to the previous frequency. Any assistance would be great!

Kind Regards,
Andy

NumberFrequencyFrequency % of total Cumulative
331490.0256277950.025628
191440.0247678020.050396
401440.0247678020.075163
251430.0245958030.099759
21410.0242518060.124011
71400.0240798070.148091
281390.0239078090.171999
351390.0239078090.195906
11380.023735810.219642
211380.023735810.243378
201370.0235638110.266942
271360.0233918130.290334
371360.0233918130.313725
31350.0232198140.336945
51350.0232198140.360165
61340.0230478160.383213
451340.0230478160.406261
81330.0228758170.429137
161330.0228758170.452012
261330.0228758170.474888
431330.0228758170.497764
41310.022531820.520296
171310.022531820.542828
411310.022531820.565359
221300.0223598210.587719
441300.0223598210.610079
151290.0221878220.632267
231290.0221878220.654455
291290.0221878220.676643
341280.0220158240.698658
91250.0214998280.720158
121250.0214998280.741658
131250.0214998280.763158
311240.0213278290.784486
391230.0211558310.805642
361220.0209838320.826625
321180.0202958380.846921
381160.019951840.866873
141150.0197798420.886653
181130.0194358450.906089
301130.0194358450.925525
421110.0190918470.944616
111100.0189198490.963536
241100.0189198490.982456
101020.017543861

<colgroup><col span="2"><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
Public Sub RandomN()
Dim colNames As New Collection
Const kPICKS = 7
Dim i As Integer, iCnt As Integer, iNum As Integer, iCol As Integer
Dim rStart As Range


On Error GoTo errRand


    'load the collection
Sheets("sheet1").Select
Range("A2").Select
While ActiveCell.Value <> ""
   colNames.Add ActiveCell.Value
   ActiveCell.Offset(1, 0).Select   'next row
Wend


  'post results
Sheets.Add
Set rStart = Range("A1")
    For i = 1 To kPICKS
      iCnt = colNames.Count
      'iNum = Int(iCnt * Rnd) + 1
      iNum = Int(1 + Rnd() * (iCnt - 1 + 1))
      
      ActiveCell.Value = colNames(iNum)
      colNames.Remove iNum
      
       ActiveCell.Offset(1, 0).Select   'next row
    Next
    
    iCol = iCol + 1
    rStart.Offset(0, iCol).Select
errRand:
Set rStart = Nothing
End Sub
 
Upvote 0
Thanks ranman256,

I'm not quite sure how to implement your code/recommendation - how would I do this in Excel?
 
Upvote 0
A​
B​
C​
D​
1​
Number
Freq
Cumu
2​
33​
149​
0​
C2: =SUM(B$1:B1)/SUM($B$2:$B$46)
3​
19​
144​
0.025628​
4​
40​
144​
0.050396​
5​
25​
143​
0.075163​
6​
2​
141​
0.099759​
7​
7​
140​
0.124011​
8​
28​
139​
0.148091​
9​
35​
139​
0.171999​
10​
1​
138​
0.195906​
11​
21​
138​
0.219642​
12​
20​
137​
0.243378​
13​
27​
136​
0.266942​
14​
37​
136​
0.290334​
15​
3​
135​
0.313725​
16​
5​
135​
0.336945​
17​
6​
134​
0.360165​
18​
45​
134​
0.383213​
19​
8​
133​
0.406261​
20​
16​
133​
0.429137​
21​
26​
133​
0.452012​
22​
43​
133​
0.474888​
23​
4​
131​
0.497764​
24​
17​
131​
0.520296​
25​
41​
131​
0.542828​
26​
22​
130​
0.565359​
27​
44​
130​
0.587719​
28​
15​
129​
0.610079​
29​
23​
129​
0.632267​
30​
29​
129​
0.654455​
31​
34​
128​
0.676643​
32​
9​
125​
0.698658​
33​
12​
125​
0.720158​
34​
13​
125​
0.741658​
35​
31​
124​
0.763158​
36​
39​
123​
0.784486​
37​
36​
122​
0.805642​
38​
32​
118​
0.826625​
39​
38​
116​
0.846921​
40​
14​
115​
0.866873​
41​
18​
113​
0.886653​
42​
30​
113​
0.906089​
43​
42​
111​
0.925525​
44​
11​
110​
0.944616​
45​
24​
110​
0.963536​
46​
10​
102​
0.982456​
47​
48​
19​
A48: =INDEX($A$2:$A$46, MATCH(RAND(), $C$2:$C$46))
49​
7​
50​
42​
51​
17​
52​
40​
53​
41​
54​
2​
55​
18​
56​
13​
57​
37​
58​
2​
59​
45​
60​
7​
61​
32​
62​
19​
63​
37​
 
Upvote 0
Thanks shg,

For some reason when I use INDEX($A$2:$A$46, MATCH(RAND(), $C$2:$C$46)) it seems to only come up with 10 each time - any ideas?
 
Upvote 0
File > Options > Formulas, > Calculation options > Workbook calculation, tick Automatic
 
Upvote 0
Glad you got it sorted.
 
Upvote 0

Forum statistics

Threads
1,216,584
Messages
6,131,561
Members
449,655
Latest member
Anil K Sonawane

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