'Written by Satendra Tiwari. Mail me at smani7dm2000@yahoo.co.in

' Refer here for the monte Carlo method

' http://www.chem.unl.edu/zeng/joy/mclab/mcintro.html

' MC Intro to calculate the value of Pi

Sub MonteCarlopi()

Dim numsims, count As Long

Dim xcord, ycord, dist, pi As Double

'Choose the number of simulations

numsims = 1000000

count = 0

For i = 1 To numsims

'Create random point in xy plane. Generate random x and y co-ordinates

xcord = 2 * Rnd() - 1

ycord = 2 * Rnd() - 1

'Calculate the distance of this point

dist = Sqr(xcord * xcord + ycord * ycord)

If dist <= 1 Then

count = count + 1

End If

Next i

'Calcualte the value of pi

pi = 4 * (count / numsims)

MsgBox "Value of pi is " + CStr(pi)

End Sub

' Refer here for the monte Carlo method

' http://www.chem.unl.edu/zeng/joy/mclab/mcintro.html

' MC Intro to calculate the value of Pi

Sub MonteCarlopi()

Dim numsims, count As Long

Dim xcord, ycord, dist, pi As Double

'Choose the number of simulations

numsims = 1000000

count = 0

For i = 1 To numsims

'Create random point in xy plane. Generate random x and y co-ordinates

xcord = 2 * Rnd() - 1

ycord = 2 * Rnd() - 1

'Calculate the distance of this point

dist = Sqr(xcord * xcord + ycord * ycord)

If dist <= 1 Then

count = count + 1

End If

Next i

'Calcualte the value of pi

pi = 4 * (count / numsims)

MsgBox "Value of pi is " + CStr(pi)

End Sub

## No comments:

## Post a Comment