Create biased random test data in VBA

Today’s post is a quick function for creating biased randoms from a list of items and weights. You might need this to create some test data, where the outcomes are not completely random.



As an example, let’s say we want to create a set of test data that closely matches a real life distribution, in this case population by continent – here’s the population distribution

Asia : 4,140 m
Africa : 994 m
Europe : 738m
North America : 528 m
South America : 385 m
Ociania : 36 m
Antartica : .004 m


Based on this distribution we want to create some random test data, that looks like this. The pie chart shows the generated distribution for 1000 rows

Code and formulas

First of all we setup a couple of lists – one with the list of possibilities, and other with the weights for each of the possibilities. You can use any relative values – they will be normalized to a percentage of the total. In this case I simply used the actual populations. Here’s my comma separated lists, placed in cells d3 and d4.

Antartica,Asia,Africa,Europe,North America,South America,Oceania

Next we enter this formula in the first data cell, and fill down=biasedRandom($D$3,$D$4)
And here is the UDF, biasedRandom

Function biasedRandom(possibilities, weights) As String
    Dim w As Variant, a As Variant, p As Variant, _
        r As Double, i As Long
    ' comes in as 2 lists
    a = Split(weights, ",")
    p = Split(possibilities, ",")
    ReDim w(LBound(a) To UBound(a))
 ' create cumulative
    For i = LBound(w) To UBound(w)
        w(i) = CDbl(a(i))
        If i > LBound(w) Then w(i) = w(i - 1) + w(i)
    Next i
' get random index
    r = Rnd() * w(UBound(w))
 ' find its weighted position
For i = LBound(w) To UBound(w)
        If (r 

For more stuff like this, see the Excel Liberation site.

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.