In: Computer Science
You have a 1 ft radius dartboard placed in the middle of a 2 ft x 2 ft square backboard. You can estimate the value of Pi by randomly throwing darts at the board, because the ratio of the darts hitting the dartboard compared to those hitting the backboard (i.e. all of them), will be equal to the ratio of the area of the dartboard to the area of the board. Or:
Dh / Dt = Adb / Ab
where:
Dh = number of darts that hit the dartboard
Dt = total number of darts thrown
Adb = Area of the dartboard
Ab = Area of the board
We can calculate Adb and Ab from the
problem statement. You should be able to rearrange the resulting
equation to solve for Pi, given the sizes of the dartboard and
board, and the number of darts thrown and number that hit the
dartboard.
Write a VBA program that will randomly generate a location (x and
y) the dart will hit, determine if it hit the dartboard (hint: make
the center of the dartboard x=0, y=0), and then estimate the value
of Pi after each thrown dart). Stop once your estimated value is
within 0.01 of the actual value of Pi (you can use 3.1416 as the
actual value and the ABS() function may be useful here). Output the
estimated value and the total number of darts thrown on the 1st
worksheet (labeled "Darts")
Recommended: Output the number of darts thrown, number of darts
that have hit, and the estimated value of Pi on a new row on your
worksheet after each dart is thrown. Also, limit the total number
of darts thrown, and exit with a message if you reach the
limit.
The VBA code is given below:
(Right click on the name of the sheet "Darts", click on View Code and paste this code)
Refer the snapshot of the code for indentation if needed.
_______________________________________________________________________________________________
Sub Rand()
Randomize 'for random number generation
'Decalre other variables as double to perform decimal
operations
Dim x As Double 'x co-ordinate of the Dart throw
Dim y As Double 'y-co-ordinate of the dart throw
Dim pie As Double 'value of estimated pie
Dim dh As Double 'no. of darts that hit
Dim dt As Double 'no. of darts that are thrown
'Initialize dh and dt with 0
dh = 0
dt = 0
While dt < 100000 And Abs(pie - 3.1416) > 0.01
'Run the while loop till 100000 trials or till the value of pie is
estimated with an error less than 0.01
x = (Int(2000 * Rnd) - 1000) / 1000
'generate random number x. Generate a number between 0 to 2000,
subtract 1000 and divide by 1000. This will give a random number
between -1 and 1 with 3 decimal points
y = (Int(2000 * Rnd) - 1000) / 1000
'generate random number y in similar fashion
distance = Sqr((x * x) + (y * y))
'this will give the distance from origin 0,0. If distance is less
than 1, then dart is hit.
If distance <= 1 Then dh = dh + 1 'if distance is less than 1
(i.e. dart is hit on the dartboard), then increase dh by 1
dt = dt + 1 'increase no. of dart throws by 1
pie = (dh / dt) * ((2 * 2) / (1 * 1)) 're-calculate value of pie
using the formula provided
Wend
If dt = 100000 Then MsgBox ("Limit for Total Number of Darts
thrown Reached")
'Display message ib dialogue box if no. of trials exceeded
'Set required values in the concerned cells
Range("B1").Value = dt
Range("B2").Value = dh
Range("B3").Value = pie
End Sub
________________________________________________________________________________________________
Snapshot of the code for reference:
The snapshot of the output is:
Please write correct description in cells in column A as required as is shown in the output.
An up-vote/thumbs-up will be greatly appreciated!