VBScript Coding Exercise – Compute PI using Monte Carlo Random Method


Monte-Carlo algorithm is a generalized method that can be used in many applications. It is a randomize approach. To  compute the mathematics constant tex_af13d5f3905a7f5cfa284795beaccdb6 VBScript Coding Exercise - Compute PI using Monte Carlo Random Method algorithms math monte carlo Monte Carlo Simulation Monte Carlo Simulation Algorithm VBA vbscript windows scripting host , we can use the method that is based on the probability.

See more posts here, here and here.

The below is a quick coding exercise for VBScript, that runs on mainly windows platform e.g. Windows Scripting Host. VBScript (not VB) is one of my favourite programming language because of: simplicity and no installation required for windows (in bundle with most Windows versions).

Also, VBScript is highly frequent used in ASP (Server side), VBA (for Microsoft Office) etc.

Monte-Carlo Compute PI – VBScript Version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
' HelloACM.com rocks
' Monte-Carlo Compute PI - VBScript Version
 
Dim cnt: cnt = 0
Dim i, x, y
Const N = 1000000
 
Randomize 
For i = 1 To N
    x = Rnd
    y = Rnd
    If x * x + y * y <= 1.0 Then
        cnt = cnt + 1
    End If
Next
 
MsgBox "PI = " & (4.0 * cnt / N)
' HelloACM.com rocks
' Monte-Carlo Compute PI - VBScript Version

Dim cnt: cnt = 0
Dim i, x, y
Const N = 1000000

Randomize 
For i = 1 To N
	x = Rnd
	y = Rnd
	If x * x + y * y <= 1.0 Then
		cnt = cnt + 1
	End If
Next

MsgBox "PI = " & (4.0 * cnt / N)

Just save the above file in *.vbs and double click (interpreted using C:\windows\system32\cscript.exe, or wscript.exe, or the same files under C:\windows\syswow64, i.e. run 32-bit interpreter under 64-bit windows).

If everything goes well, you will see a messagebox pop up:

50beb4c245f7567c244f6a993a49c4a7.jpg VBScript Coding Exercise - Compute PI using Monte Carlo Random Method algorithms math monte carlo Monte Carlo Simulation Monte Carlo Simulation Algorithm VBA vbscript windows scripting host

Monte Carlo Simulation Algorithms to compute the Pi based on Randomness:

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
804 words
Last Post: It takes 5 hours on a 8-bit famicom clone (SB2000) to compute 80 decimal places of PI
Next Post: Flash Your Webpage Title - Javascript Code Snippet

The Permanent URL is: VBScript Coding Exercise – Compute PI using Monte Carlo Random Method

Leave a Reply