Question

In: Computer Science

You have a 1 ft radius dartboard placed in the middle of a 2 ft x...

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.

Solutions

Expert Solution

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!


Related Solutions

1) A circle with a radius of 0.3 meters is placed in the x-y plane in...
1) A circle with a radius of 0.3 meters is placed in the x-y plane in a magnetic field directed in the -z direction. It is wound once and its resistance is 6 Ohms. The initial magnetic field is 0.8 Tesla. The magnetic field is then changed in 0.6 s and the average induced current is 0.04 amps running counter-clockwise. First, find the final magnetic field to needed to induce this average current. Then, the magnetic field (still in the...
1) A circle with a radius of 0.3 meters is placed in the x-y plane in...
1) A circle with a radius of 0.3 meters is placed in the x-y plane in a magnetic field directed in the -z direction. It is wound once and its resistance is 7 Ohms. The initial magnetic field is 0.6 Tesla. The magnetic field is then changed in 0.7 s and the average induced current is 0.05 amps running counter-clockwise. First, find the final magnetic field to needed to induce this average current. Then, the magnetic field (still in the...
A building is next to a horizontal curve with a 132 ft. radius on a 2...
A building is next to a horizontal curve with a 132 ft. radius on a 2 lane rural highway. The inside lane is 10 feet wide, and the building is 6 feet from the shoulder, which is 4 feet wide. a) What sight distance is available to the driver at the critical point? b) Assuming design values for driver perception and reaction time and longitudinal deceleration, What is the maximum speed that would provide appropriate stopping sight distance? PLEASE EXPLAIN...
A circle with a radius of 0.3 meters is placed in the x-y plane in a...
A circle with a radius of 0.3 meters is placed in the x-y plane in a magnetic field directed in the -z direction. It is wound once and its resistance is 8 Ohms. The initial magnetic field is 0.5 Tesla. The magnetic field is then changed in 0.7 s and the average induced current is 0.05 amps running counter-clockwise. First, find the final magnetic field to needed to induce this average current. Then, the magnetic field (still in the -z...
Problem 2 : If the slab is 25 ft wide x 50 ft long x 18...
Problem 2 : If the slab is 25 ft wide x 50 ft long x 18 inches deep with the total steel reinforcement of 15,000 ft of #5 bar. Find the quantity of concrete required (in cubic yards). The capacity of concrete truck is 9 cubic yards. Assume waste factor as 10%. Density of steel is 490 lbs/cubic feet. Problem 3 : If the slab is 75 ft wide x 100 ft long x 9 inches deep with the total...
Problem 2: A 3 ft x 5 ft (B x L) rectangular footing is shown in...
Problem 2: A 3 ft x 5 ft (B x L) rectangular footing is shown in following figure. Using generalized bearing equation (with shape, depth and inclination factors), determine the allowable (safe) load that the foundation carry. Given: gamma = 115 lb/ft3 , c’= 1100 lb/ft2 , phi’ = 35°, Df =3.5 ft, Ground water table is located at 5 ft below ground level. Assume factor of safety = 3. Assume gammasat = 127.4 lb/ft
1. A living room is 15 ft x 19 ft. The walls of the room are...
1. A living room is 15 ft x 19 ft. The walls of the room are 8.5 ft tall. Calculate the total area of the West wall? How many rolls of wallpaper will you need to cover the wall. One roll will cover 44 square feet What will be the cost of the wallpaper? ($14.99 for a pack of 2 rolls) 2. Calculate the following for the remaining walls Calculate the total area of the North Wall. (Note: You are...
What is the Maclaurin's series for (-2x/(1+x^2)^2) and its radius of convergence.
What is the Maclaurin's series for (-2x/(1+x^2)^2) and its radius of convergence.
+1 nc is placed at the origin and -1 nc is placed on the x-axis 1...
+1 nc is placed at the origin and -1 nc is placed on the x-axis 1 meter away. Find the total Electric field in components form and magnitude angle form for points given P1 = (0,1) namely the point with coordinate x = 0 y = 1 P2 = (0.5, 1) P3 = (1,1) P4 = (1.5, 1)
A filled water bed has the dimensions 8.0 ft x 7.0 ft x 0.75 ft. Determine...
A filled water bed has the dimensions 8.0 ft x 7.0 ft x 0.75 ft. Determine the mass of water, in kilograms, needed to fill the bed. Density of water = 1.00 g/cm^3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT