<< Statistics`ContinuousDistributions`
<< Statistics`DataManipulation`
<< Graphics`Graphics`

n = 4;

icrange = {.25,1};

failrange = {10,40};

acceptrange = {1,2};

\(CreateRates[ic_List, fails_List, accept_List] := 
	
      Module[
			{icrates = 
            Table[Random[Real, {1/ic[\([2]\)], 1/ic[\([1]\)]}], {i, n}, {j, 
                n - 1}], 
			failrates\  = 
            Table[Random[Real, {1/fails[\([2]\)], 1/fails[\([1]\)]}], {i, 
                n}], \ acceptrates = 
            Table[Random[Real, {1/accept[\([2]\)], 1/accept[\([1]\)]}], {i, 
                n}], 
			rates\  = \ Table[0, {n}]}, 

		Do[
          rates[\([i]\)]\  = \ 
						Append[
              Insert[icrates[\([i]\)], failrates[\([i]\)], 
                i], 
											acceptrates[\([i]\)]], {i, 1, 
            n}]; 
		rates]\)

WhichOne[probabilities_] :=

	Module[{event, eventcheck = 0,m = 0, problist},

		event = Random[];

		

		problist = Flatten[probabilities];

		While[eventcheck  < event,

			m = m+1;

			eventcheck = eventcheck + problist[[m]]

			];

		If[Mod[m,n+1]==0,

			to = n+1; from = Quotient[m,n+1],

			to = Mod[m,n+1];from = Quotient[m,n+1]+1;

			];

		(*Print["to = ",to,"  from = ",from];*)

		]

\(	\(stmp = OpenWrite["simlagtimes"];\)\)
\(Do[
time = 0; 
descendants\  = \ Table[0, {n}, {n}]; 

      failures\  = \ \(failuretime = Table[0, {n}]\); 
eventno\  = \ 0; 
	
      exprates = CreateRates[icrange, failrange, acceptrange]; 
\ 
      mu = Apply[Plus, Flatten[exprates]]; 
\ 
      eventprobs\  = \ exprates/mu; 

      While[time\  < 200, 
	\(eventno++\); 
	
        time = time + Random[ExponentialDistribution[mu]]; 
	
        WhichOne[eventprobs]; 
	
        Which[to\  == \ 
            from, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ 
            failure\ occured\ *) 
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[
            Apply[Plus, failures] == 
              0, \ \  (*no\ other\ failures*) 
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants\  = \ 
              Table[0, {n}, {n}]	 (*reset\ *) 
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]; 
	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 	If[
            failures[\([to]\)]\  != \ 
              1, \ \ \ 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*no\ previous\ error\ in\ process*) 
		\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([to]\)]\  = \ 
              Table[0, {n}]; 	 (*\ 
              reset\ *) 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ failures[\([to]\)]\  = \ 1; \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ 
              record\ failure\ *) 		
		\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ failuretime[\([to]\)]\  = \ 
              time
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], \ 		\ \ \ \ \ 
		\ \ 
		\ \ \ \ \ \ \ \ \ to\  == \ 
            n + 1, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*Acceptance\ test\ occured\ *) 
			\ \ \ \ \ \ \ \ \ \ \ \ If[
            failures[\([from]\)]	 == \ 
              0, 
				\ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([from]\)]\  = \ Table[
                0, {n}]
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\ ; \ \ 							 (*no\ failure - reset*) 
			\ \ \ \ \ \ \ \ \ \ \ Do[
            If[failures[\([i]\)]\  == \ 
                  1\  && 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \((descendants[\([i, from]\)]\  == \ 
                      1\  || 
					\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 	i\  == \ from)\), 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Write[
                stmp, time - 
                  failuretime[\([i]\)]]; 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([i]\)]\  = \ 
                Table[0, {n}]; 
					\ \ \ \ \ \ \ \ \ \ \ \ \ failuretime[\([i]\)]\  = \ \(failures[\([i]\)] = 
                  0\)
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], {i, 1, 
              n}
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], 
		
			\ \ True, \  (*\ 
            connection\ occured\ *) 
			\ \ \ \ \ \ \ \ descendants[\([from, to]\)]\  = \ 1; 
			\ \ \ \ \ \ \ \ Do[
            If[descendants[\([j, from]\)]\  == \ 
                1, 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[
                j\  != \ from\  && \ 
                  j\  != \ 
                    to\  && \ 
						\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([j, to]\)]\  == \ 
                    0, \ 
				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ descendants[\([j, to]\)]\  = \ 
                  1
			\ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], {j, 1, 
              n}
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ ]
	]\ \ \ \ \ \  (*\ 
          End\ of\ Which\ *) 
]\ , \ \ \ \ \ \ \ \ \ \  (*\ 
        End\ of\ While\ *) 
{5}]; \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  (*\ 5\ simulations\ *) 
Close[stmp];\)

lagtime  =  ReadList["simlagtimes"];
dx = 0.2;
\(	\ \ \ \(freq\  = \ 
        BinCounts[lagtime, {0, Max[lagtime], dx}];\)\)
      midpoints  =  
        Range[dx/2, Max[lagtime] + dx/2, dx];
\(\ \ \ \ \ \ BarChart[
		\ \ \ \ \ \ \ \ \ Transpose[{freq, 
          midpoints}], 
      PlotRange -> All, 
						PlotLabel\  -> 
        FontForm["Latency times", \ {"Times-Roman", 12}]]\)

lagtime  =  ReadList["simlagtimes"]

lagtime  =  ReadList["simlagtimes"];
dx = 0.2;
\(	\ \ \ \(freq\  = \ 
        BinCounts[lagtime, {0, Max[lagtime], dx}];\)\)
      midpoints  =  
        Range[dx/2, Max[lagtime] + dx/2, dx];
\(\ \ \ \ \ \ BarChart[
		\ \ \ \ \ \ \ \ \ Transpose[{freq, 
          midpoints}], 
      PlotRange -> All, 
						PlotLabel\  -> 
        FontForm["Latency times", \ {"Times-Roman", 12}]]\)