How to summarize repeated calculations using rand()

roscoe

Well-known Member
Joined
Jun 4, 2002
Messages
1,062
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm trying to compare two differential financial options, both based on the same (but randomly determined) market return rates. One option can't lose money but is capped on gains, whereas the other can make or lose as much as the market rates do. At the end of 10 years I'd like to compare the results. That in itself is not so hard.

Using the rand() function the market numbers recalculate constantly which subsequently changes the results. Here's the kicker. What I'd like to do is run 10, 100, 1000, whatever, iterations of random numbers and determine an average result for each scenario. Right now I would have to record one, then recalc, then record those results, etc...

Obviously there has to be an easier way...looking for ideas.

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I don't see any solution other than recording intermediate results to calculate the average.

Assuming that a random number is entered in A1 and the result is obtained in D1, you can implement such a code:
VBA Code:
Sub AAA()
    Dim v As Variant
    Dim Cnt As Long
    Dim i As Long
    Dim Result As Double

    Cnt = Application.InputBox(Prompt:="How many iterations?", Type:=1)

    If Cnt < 1 Then Exit Sub

    Application.ScreenUpdating = False

    ReDim v(1 To Cnt)

    Randomize

    For i = 1 To Cnt
        Range("A1").Value = Rnd
        v(i) = Range("D1").Value
    Next i

    Result = Application.Sum(v) / Cnt

    Range("A1").Value = Result

    Application.ScreenUpdating = True

    MsgBox "The average result for " & Cnt & " iterations is: " & Range("D1").Value

End Sub
Artik
 
Upvote 0
Thanks, that was kinda what I was thinking. But because I was rusty in VBA I ended up just creating 10,000 lines of identical (but randomized) data and built a table aggregating that data.
 
Upvote 0
I went back and used your code with some added modifications to handle multiple scenarios at once. Works great, thanks!
 
Upvote 0

Similar threads

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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