/*
 * @(#) smt2_5.c - Program for calculation of effectiveness of the radar
 * station as a system of queuing using the method of statistical tests
 * (Task 2_5 at the Special Military Training course).
 * (c) 1997 Ivan Maidanski <ivmai@chat.ru> http://ivmai.chat.ru
 * Freeware program source. All rights reserved.
 **
 * Language: ANSI C
 * Tested with: Borland C++ v3.1
 * Last modified: 1997-05-04 17:00:00 GMT+04:00
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

#define NN 5000
#define MAX_N 10

float primary_stream(float lambda)
{
 int xi;
 while (!(xi=rand()));
 return (-log((float)xi/RAND_MAX)/lambda);
}

int main(void)
{
 int n;
 float lambda,tau;
 int p[MAX_N+1];
 float t[MAX_N];
 float dt;
 int i,j,s;
 printf("n= ");
 scanf("%d",&n);
 printf("Lambda= ");
 scanf("%f",&lambda);
 printf("ServiceTime= ");
 scanf("%f",&tau);
 printf("\n");
 randomize();
 for (i=0;i<=n;i++)
  p[i]=0;
 for (i=0;i<n;i++)
  t[i]=0.0;
 for (j=0;j<NN;j++)
 {
  dt=primary_stream(lambda);
  for (i=0;i<n;i++)
   t[i]= t[i]>dt ? t[i]-dt : 0.0;
  for (i=0,s=0;i<n;i++)
   if (t[i])
    s++;
  p[s]++;
  for (i=0;i<n;i++)
   if (!t[i])
     break;
  if (i<n)
   t[i]=tau;
 }
 for (i=1,s=0;i<=n;i++)
  s+=p[i]*i;
 for (i=0;i<=n;i++)
  printf("P%d= %f\n",i,(float)p[i]/NN);
 printf("M[busy]= %f\n",(float)s/NN);
 printf("P[break]= %f\n",(float)p[n]/NN);
 printf("\n");
 return 0;
}
