Results 1 to 8 of 8
Thread: Have I done this exercise right?
 09272010, 04:34 PM #1Senior 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.
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(); }
 09272010, 05:01 PM #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?
 09272010, 05:17 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,944
 Blog Entries
 7
 Rep Power
 22
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
 09282010, 04:57 PM #4Senior 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(); }
 09282010, 05:13 PM #5Senior 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; 09282010 at 05:16 PM.
 09282010, 05:22 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.
 09282010, 05:34 PM #7Senior 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(); }
 09282010, 05:54 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,944
 Blog Entries
 7
 Rep Power
 22
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
Similar Threads

Exercise for java 3d
By armiri in forum Java 2DReplies: 2Last Post: 05132010, 11:14 PM 
Exercise for java 3d
By armiri in forum Java SoftwareReplies: 3Last Post: 05132010, 11:13 PM 
I/O exercise
By Feldom in forum New To JavaReplies: 1Last Post: 10282007, 05:48 PM 
help with exercise
By e_as're in forum New To JavaReplies: 3Last Post: 09252007, 10:14 AM 
help with an exercise calcuting tax
By e_as're in forum New To JavaReplies: 7Last Post: 08012007, 03:17 AM
Bookmarks