kisslaszlo123
New Member
- Joined
- Apr 16, 2020
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
Hello,
I try to code a VBA Monte Carlo simulation that calculates the value of Pi with the help of a linear congruential generator. However something is wrong with my sub. Can someone please take a look and explain me what is the problem here?
Option Explicit
Sub mcarlo()
Dim i
Dim seed As LongLong
Dim x As LongLong
Dim period As Variant
Dim myarray() As LongLong
Dim counter As LongLong
Dim coord1 As LongLong
Dim coord2 As LongLong
Dim distance As LongLong
Dim pi As LongLong
Dim j As Variant
Dim k As Variant
period = 100
seed = 6
ReDim myarray(2 * period)
x = seed
For i = 1 To 2 * period
x = (4569872 * x + 1) Mod 45239875123
myarray(i) = x / 100000000
Next i
counter = 0
For j = 1 To period
coord1 = myarray(j)
For k = period + 1 To 2 * period
coord2 = myarray(k)
distance = Sqr(coord1 * coord1 + coord2 * coord2)
If distance <= 452.398 Then
counter = counter + 1
End If
Next k
Next j
pi = 4 * (counter / period)
MsgBox pi
End Sub
I try to code a VBA Monte Carlo simulation that calculates the value of Pi with the help of a linear congruential generator. However something is wrong with my sub. Can someone please take a look and explain me what is the problem here?
Option Explicit
Sub mcarlo()
Dim i
Dim seed As LongLong
Dim x As LongLong
Dim period As Variant
Dim myarray() As LongLong
Dim counter As LongLong
Dim coord1 As LongLong
Dim coord2 As LongLong
Dim distance As LongLong
Dim pi As LongLong
Dim j As Variant
Dim k As Variant
period = 100
seed = 6
ReDim myarray(2 * period)
x = seed
For i = 1 To 2 * period
x = (4569872 * x + 1) Mod 45239875123
myarray(i) = x / 100000000
Next i
counter = 0
For j = 1 To period
coord1 = myarray(j)
For k = period + 1 To 2 * period
coord2 = myarray(k)
distance = Sqr(coord1 * coord1 + coord2 * coord2)
If distance <= 452.398 Then
counter = counter + 1
End If
Next k
Next j
pi = 4 * (counter / period)
MsgBox pi
End Sub