Random value assign with weightage

avilashm

New Member
Joined
Apr 28, 2019
Messages
1
[FONT=garamond, serif]I have a sample size of 30. I need an excel function in which I can assign " Random Values " (say a question has 4 options and need to assign 1 to 4 randomly) based on the weight to each individual from the sample.[/FONT]
[FONT=garamond, serif]
[/FONT]
[FONT=garamond, serif]Means Option 1: 45%[/FONT]
[FONT=garamond, serif] Option 2: 12%[/FONT]
[FONT=garamond, serif] Option 3: 30%[/FONT]
[FONT=garamond, serif] Option 4: 13%[/FONT]
[FONT=garamond, serif]
[/FONT]
[FONT=garamond, serif]How to assign random values (1,2,3,4) with its respective percentages/weight.[/FONT]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This macro will produce results like those below using columns A & B. If there are sufficient "Trials" run the options will appear randomly, but in proportion to their respective weights.
Code:
Sub avilashm()
Dim OptionWeight(1 To 4), c As Variant, Trial As Variant
Const TrialsCount As Long = 100 'change to suit
Application.ScreenUpdating = False
Range("A:B").ClearContents
OptionWeight(1) = 0.45   'Change to suit
OptionWeight(2) = 0.12
OptionWeight(3) = 0.3
OptionWeight(4) = 0.13
If Evaluate(Join(OptionWeight, "+")) <> 1 Then
    MsgBox "Option weights must sum to 1 - try again"
    Application.ScreenUpdating = True
    Exit Sub
End If
Range("A1:B1").Value = Array("Trial No.", "Option No.")
For Each c In Range("B2:B" & TrialsCount + 1)
    Ct = Ct + 1
    c.Offset(0, -1).Value = Ct
Again:  Trial = Application.RandBetween(1, 4)
        If Application.CountIf(Range("B$1:B" & c.Row - 1), Trial) < TrialsCount * OptionWeight(Trial) Then
            c.Value = Trial
        Else
            GoTo Again
        End If
Next c
End Sub
Excel Workbook
ABCDEFG
1Trial No.Option No.1234
21345123013
3220.450.120.30.13
431
541
652
763
873
981
1091
11103
12114
13123
14134
15141
16152
17164
18172
19181
20191
21204
22214
23221
24232
25242
26251
Sheet9
 
Upvote 0
After looking at this a bit more I modified it to get something that randomizes the weighted options without affecting the weights. This modification uses columns A:C. Replace the code in post #2 with this:
Code:
Sub avilashm()
Dim OptionWeight(1 To 4), c As Variant, Trial As Variant
Const TrialsCount As Long = 100 'change to suit
Application.ScreenUpdating = False
Range("A:C").ClearContents
OptionWeight(1) = 0.45   'Change to suit
OptionWeight(2) = 0.12
OptionWeight(3) = 0.3
OptionWeight(4) = 0.13
If Evaluate(Join(OptionWeight, "+")) <> 1 Then
    MsgBox "Option weights must sum to 1 - try again"
    Application.ScreenUpdating = True
    Exit Sub
End If
Range("A1:C1").Value = Array("Trial No.", "Option No.", "Temp")
For Each c In Range("B2:B" & TrialsCount + 1)
    Ct = Ct + 1
    c.Offset(0, -1).Value = Ct
Again:  Trial = Application.RandBetween(1, 4)
        If Application.CountIf(Range("B$1:B" & c.Row - 1), Trial) < TrialsCount * OptionWeight(Trial) Then
            c.Value = Trial
        Else
            GoTo Again
        End If
Next c
'random scramble of options
With Range("B1:C" & TrialsCount + 1)
    Randomize
    .Cells(2, 2).Formula = "= RAND()"
    With .Columns(2).Offset(1, 0).Resize(.Columns(2).Rows.Count - 1)
        .FillDown
        .Calculate
        .Value = .Value
    End With
    .Sort key1:=[C2], order1:=xlAscending, Header:=xlYes
    .Columns(2).ClearContents
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,098
Members
453,021
Latest member
Justyna P

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