Java Reference
In-Depth Information
public static int random(int m, int n) {
//returns a random integer from m to n, inclusive
return (int) (Math.random() * (n - m + 1)) + m;
} //end random
} //end class SimulateQueue
A sample run of Program P6.6 is shown here:
How many counters? 2
How many customers? 10
Start Service Wait
Customer Arrives Service Counter Time Departs Time
1 3 3 1 8 11 0
2 7 7 2 9 16 0
3 10 11 1 9 20 1
4 11 16 2 4 20 5
5 14 20 1 5 25 6
6 19 20 2 9 29 1
7 23 25 1 7 32 2
8 26 29 2 8 37 3
9 29 32 1 7 39 3
10 33 37 2 6 43 4
As you can see, the waiting time is reasonably short. However, if you run the simulation with 25 customers, you
will see that the waiting time increases appreciably. What if we added another counter? With simulation, it's easy to
test the effect of this without actually having to buy another machine or hire another employee.
In this case, all we have to do is enter 3 and 25 for the number of counters and customers, respectively. When
we do, we will find that there is very little waiting time. We urge you to experiment with different data—counters,
customers, arrival times, and service times—to see what happens.
6.10 Estimating Numerical Values Using Random Numbers
We have seen how random numbers can be used to play games and simulate real-life situations. A less obvious use
is to estimate numerical values that may be difficult or cumbersome to calculate. We will show how to use random
numbers to estimate the square root of a number and p (pi).
6.10.1 Estimating 5
We use random numbers to estimate the square root of 5 based on the following:
It is between 2 and 3.
x is less than 5 if x 2 is less than 5.
Random numbers, with fractions, between 2 and 3 are generated. A count is kept of those
numbers that are less than 5 .
 
Search WWH ::




Custom Search