Once pseudorandom numbers are generated, the most common error I see when I look at finance students who are starting to implement simulations into their analysis is for them to generate a series of pseudorandom numbers using Excel's prebuilt functions and assume that those numbers can be directly used to test the probability of events taking place. We will delve into working with probabilities in simulations later in this chapter, but for now what we have to realize is that a pseudorandom number can be generated from differing numerical distributions.
The best way to understand what it means to generate a pseudorandom number from different numerical distributions is to start with the outputs from the series of pseudorandom numbers and create a histogram with the numbers. In the Model Builder sections in this chapter, we will look at how to create these outputs, but for now we should understand that pseudorandom numbers generated assuming different distributions will have very different characteristics. Figure 2.1 shows three histograms generated from uniform, normal, and lognormal distributions, as labeled.
As was just shown in Figure 2.1, a pseudorandom number can take many forms depending on the distribution assumed. In this Model Builder exercise we will start with the most basic pseudorandom generation on the Excel sheet using the RAND function and show how to create a histogram to demonstrate that the RAND function draws numbers between 0 and 1 from a uniform distribution. Note that the file MB2.0 will be used for this Model Builder exercise, but only one sheet is dedicated to it.
=COUNTIF($C$5:$C$104,“>”&E6)-COUNTIF($C$5:$C$104,“>”&F6)
The COUNTIF function counts the number of cells that meet a certain criteria. The criteria parameter can be any logical statement, but it is tricky to enter. If we want to say greater than a referenced value then we have to put the “greater than” symbol (>) in double quote marks and then use the ampersand to connect the greater than symbol to a referenced cell, which contains a value. The first part of the formula above counts the number of cells that are absolutely greater than the first bucket value (0 in this case). To find out the number of cells that are WITHIN the next bucket range we subtract out the number of cells that are greater than the next interval in the bucket range. Copy and paste this formula over the range F7:O7. Also, we may want to check to make sure we are counting all 100 cells and should put a quick sum check in place. Enter “=SUM(F7:O7)” in Q7. Figure 2.2 shows the area that should be created.
Since it is impractical to implement simulation directly on the Excel sheet and since many examples in this book will use VBA, we should also cover how pseudorandom numbers are generated in VBA. Beyond a difference in the function spelling, there are additional steps and nuances regarding the seed state we should be aware of in VBA. To understand these differences, Model Builder 2.2 will recreate the first two steps of Model Builder 2.1 in VBA. The histogram and chart creation are best done on the Excel sheet.
Sub UniformRandGen() Dim rCt As Integer, rMax As Integer rMax = 100 ReDim uArray(1 To rMax, 0)
Randomize
The Randomize statement initializes the pseudorandom number generator in VBA. If it is used on it is own as in the example code then the system timer is set as the seed state.
For rCt = 1 To rMax uArray(rCt, 0) = Rnd() Next rCt
This code starts a For Loop that will run for 100 loops (given the value set for rMax). Within the For Loop the array, uArray, is filled with a value provided by the Rnd function. The Rnd function creates a pseudorandom number from a uniform distribution. If it is entered with no parameters after it, as in the example code, Rnd generates the next pseudorandom number in the sequence. While there are a few combinations of using Randomize and Rnd with and without parameters, the way it is set up currently is probably the most useful.
The second most useful option would be to have the same set of pseudorandom numbers repeat. This can be done using a negative number parameter in the Rnd function. Replace the following line of code:
uArray(rCt, 0) = Rnd()
with:
uArray(rCt, 0) = Rnd(-rCt)
Range(“rng_VBAuRnd”) = uArray End Sub