Java Reference
In-Depth Information
21 */
22 public void drop()
23 {
24 double ylow = 2 *
generator.nextDouble();
25 double angle = 180 *
generator.nextDouble();
26
27// Computes high point of needle
28
29 double yhigh = ylow +
Math.sin(Math.toRadians(angle));
30 if (yhigh >= 2) hits++;
31 tries++;
32 }
33
34 /**
35Gets the number of times the needle hit a line.
36 @return the hit count
37 */
38 public int getHits()
39 {
40 return hits;
41 }
42
43 /**
44 Gets the total number of times the
needle was dropped.
45 @return the try count
46 */
47 public int getTries()
48 {
49 return tries;
50 }
51
52 private Random generator;
53 private int hits;
54 private int tries;
55 }
259
260
ch06/random2/NeedleSimulator.java
1 /**
2This program simulates the Buffon needle experiment
Search WWH ::




Custom Search