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.

Example

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

Result

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
.004,4140,994,738,528,385,36

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 <= w(i)) Then
            biasedRandom = p(i)
            Exit Function
        End If
    Next i
   
End Function

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

(function() { var po = document.createElement(‘script’); po.type = ‘text/javascript’; po.async = true; po.src = ‘https://apis.google.com/js/plusone.js’; var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(po, s); })();

Author: bm082975

Leave a Reply

Your email address will not be published. Required fields are marked *