/** We are given a queueing network
                           t1
		   ---+   +-+      1-r
	----->X---->|||-->|A|--->X---->
              ^    ---+   +-+    |r
              |    t2            |
              |    +-+   +---   p|
              |<---|B|<--|||<----X
              |    +-+   +---    |1-p
              |    t3            |
	      |    +-+   +---    |
              +<---|C|<--|||<----+
		   +-+   +---
    A is the time spent on the first server, B the time spent on the 
    second, and C on the third.
    r and p are probabilities of taking the specified directions.
    What is the expected response time?
    In fact if we analize the network formally we find the formula
	t1/(1-r) + (p*t2+(1-p)*t3)*r/(1-r)
    and substituting t1=10, t2=20, t3=30, r=0.5, p=0.75 we get 42.5.
    Running the program gave me a very close result: 41.859
*/

public class Lab1s08Performance
{
    public static final int NUMBER_OF_TRIES = 10000;
    public static final int NUMBER_OF_LOOPS = 30;

    public static void main(String[] args) {
	double t1 = 10;
	double t2 = 20;
	double t3 = 30;
	double r = 0.5;
	double p = 0.75;
	double total = 0.0;
	for (int k = 0; k < NUMBER_OF_TRIES; k++) {
	    double response = t1;
	    for (int loop = 0; loop < NUMBER_OF_LOOPS; loop++) {
		if (Math.random() > r) break;
		if (Math.random() < p)
		    response += t1 + t2;
		else
		    response += t1 + t3;
	    }
	    total += response;
	}
	System.out.println("Average response time = " + total/NUMBER_OF_TRIES);
    }
}

