# Thread: Have I done this exercise right?

1. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Have I done this exercise right?

I have done this exercise from the book "Art and Science of Java".I believe I have done it right.Can any body point out If I have not resolved the exerience.

The exercise is
Although it is often easiest to think of random numbers in the context of games of chance, they have other, more practical uses in computer science and mathematics. For example, you can use random numbers to generate a rough approximation of the constant π by writing a simple program that simulates a dart board. Imagine that you have a dart board hanging on your wall. It consists of a circle painted on a square backdrop, as in the following diagram:(Diagram is missing)

If you throw darts at this board in a random fashion, some will fall inside the circle, but some will fall outside. If the tosses are truly random, the ratio of the number of darts that land inside the circle to the total number of darts hitting the square should be roughly equal to the ratio between the two areas. The ratio of the areas is independent of the actual size of the dart board, as illustrated by the following formula:

darts falling inside the circle
darts falling inside the square ≅
area of the circle
area of the square = πr 2
4r 2 = π
4

To simulate this process in a program, imagine that the dart board is drawn in the standard Cartesian coordinate plane you learned about in high school. The process of throwing a dart randomly at the square can be modeled by generating two random numbers, x and y, each of which lies between 1 and 1. This (x, y) point always lies somewhere inside the square. The point (x, y) lies inside the circle if
⎯√⎯x2⎯ +⎯ y2 < 1
This condition, however, can be simplified considerably by squaring each side of the inequality, which gives the following more efficient test:
x2 + y2 < 1 If you perform this simulation many times and compute the fraction of darts that fall within the circle, the result will be somewhere in the neighborhood of π/4.Write a program that simulates throwing 10,000 darts and then uses the simulation technique described in this exercise to generate and display an approximate value of π. Dont worry if your answer is correct only in the first few digits. The strategy used in this problem is not particularly accurate, even though it occasionally proves useful as a technique for making rough approximations. In mathematics, this technique is called Monte Carlo integration, after the gambling center that is the
capital city of Monaco.
My code
Java Code:
```

/*File:RandomDart.java
* */
import acm.program.*;
import acm.util.*;

public class RandomDart extends ConsoleProgram{

public void run(){
int total =0;       /* total number of darts hitting the circle initialised to zero*/
for (int i=0;i<=10000; i++){

int x = rgen.nextInt(-1,1);
int y = rgen.nextInt(-1,1);

if (x*x+y*y<1)  /*Formula to find if dart hit the circle*/

total+=1; /*Adds how many times dart hit the circle*/

}
println("Dart hitting the circle:"+total);

}

private RandomGenerator rgen = new RandomGenerator();
}```
Thank you mates 2. ## Can you write the program using JDK classes instead of using the acm package?

How does anyone determine if the program works correctly? Would the print out show that? 3. ## Duh, the ratio of C/S (C == darts in the circle, S == darts in the square) equals pi/4; why not display C/S*4 so you can see the approximation of pi? Much more interesting, especially when you increase S (which is also the total number of darts).

kind regards,

Jos 4. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Is it right now ?

Java Code:
```/*File:RandomDart.java
* */
import acm.program.*;
import acm.util.*;

public class RandomDart extends ConsoleProgram{

public void run(){
int total =0;       /* total number of darts hitting the circle initialised to zero*/
for (int i=0;i<=10000; i++){

int x = rgen.nextInt(-1,1);
int y = rgen.nextInt(-1,1);

if (x*x+y*y<1)  /*Formula to find if dart hit the circle*/

total+=1;         /*Adds how many times dart hit the circle*/

int grandtotal=total;

}

println("Dart hitting the circle:"+total);

println("The value of pi is"+valueofpi(total));}

private double valueofpi(double total){   //method to calculate value of pi
double valueofpi=0;                     // value of pi = total no.dart in circle/total no. of darts * 4
valueofpi=total/10000*4;

return valueofpi;}

private RandomGenerator rgen = new RandomGenerator();
}``` 5. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Thank you guys.I found the answer code written by they author of the book, Eric Roberts himself on the internet.And here is the final code:

Java Code:
```/*File:RandomDart.java
* */
import acm.program.*;
import acm.util.*;

public class RandomDart extends ConsoleProgram{

public void run(){
int total =0;       /* total number of darts hitting the circle initialised to zero*/
for (int i=0;i<=10000; i++){

Double x= rgen.nextDouble(-1.0,1.0);
Double  y = rgen.nextDouble(-1.0,1.0);

if (x*x+y*y<1)  /*Formula to find if dart hit the circle*/

total+=1;		 /*Adds how many times dart hit the circle*/

}

println("Dart hitting the circle:"+total);

println("The value of pi is"+valueofpi(total));}

private double valueofpi(double total){   //method to calculate value of pi
double valueofpi=0;                     // value of pi = total no.dart in circle/total no. of darts * 4
valueofpi=(total*4)/10000;

return valueofpi;}

private RandomGenerator rgen = new RandomGenerator();
}```
Last edited by ccie007; 09-28-2010 at 05:16 PM. 6. ## Do you get the expected answer?
I think your rounding of the double random numbers to int will distort the values and give incorrect results. 7. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## I think I got the right answer dude because it looks very similar to the code written by the author which is
Java Code:
```/* * File: PiApproximation.java * -------------------------- *
*  This program computes an approximation to pi by simulating *
*   a dart board, as described in Chapter 6, Programming Exercise 3 * of "The Art and Science of Java".
*   The general technique * is called Monte Carlo integration. */
import acm.program.*;
import acm.util.*;
public class piapprox extends ConsoleProgram {
/* Number of darts to throw. */
private static final int NDARTS = 10000;
public void run()
{ int inside = 0; for (int i = 0; i < NDARTS; i++)
{ double x = rgen.nextDouble(-1.0, +1.0);
double y = rgen.nextDouble(-1.0, +1.0); /* Consider circle of radius = 1, centered at (0, 0) */
if (((x * x) + (y * y)) < 1.0) { inside++; } } /* * Note: area of circle = PI * r * r = PI * 1 * 1 = PI * area of square = side * side = 2 * 2 = 4 * So, PI/4 is the fraction of darts landing in circle: * darts in circle = NDARTS * PI/4 * PI = (4 * darts in circle)/NDARTS */
double pi = (4.0 * inside) / NDARTS; println("Pi is approximately " + pi); } /* Private instance variables */

private RandomGenerator rgen = new RandomGenerator();
}``` 8. ## I see one more weakness: both the methods run() and valueofpi() need to know the total value of runs; if you change it in the run() method you have to manually change it in the other method. (there's no problem with int divisions anywhere, i.e. the int value is cast to a double value by the compiler when it is passed to the valueofpi() method).

kind regards,

Jos #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•