Random Number Generation and Single-Server Simulation

 

排隊,隨機,模擬;

Note that the theoretical answer for the mean queue length seen by an arriving customerin this system is given by h/(a–h) where h is the mean service time of a customer, and a is the mean time between arrivals of successive customers.

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <limits.h>
  5. #define NEW(type) (type *) malloc(sizeof(type))
  6. #define ARRIVAL 1
  7. #define DEPARTURE 2
  8. /* Event by event simulation of a single server queue with infinite
  9. waiting room */
  10. int
  11.     gmt, /* absolute time */
  12.     q, /* number of customers in the system */
  13.     narr, /* number of arrivals */
  14.     q_sum, /* sum of queue lengths at arrival instants */
  15.     iat, /* mean interarrival time */
  16.     service_time, /* mean holding time */
  17.     total_events; /* number of events to be simulated */
  18. long
  19.     seed; /* seed for the random number generator */
  20. typedef struct schedule_info *point;
  21. typedef struct schedule_info{
  22.     int time; /* Time that event occurs */
  23.     int event_type; /* Type of event */
  24.     point next; /* Pointer to next item in list */
  25. } EVENTLIST;
  26. point head,tail; 
  27. int act();
  28. int negexp(int);
  29. void arrival();
  30. void departure();
  31. void schedule(intint);
  32. void sim_init(); 
  33. /**********************************************************************/
  34. void main()
  35. {
  36.     sim_init();
  37.     while (narr < total_events){
  38.         switch (act()){
  39. case ARRIVAL:
  40.     arrival();
  41.     break;
  42. case DEPARTURE:
  43.     departure();
  44.     break;
  45. default:
  46.     printf("error in act procedure/n");
  47.     exit(1);
  48.     break;
  49.         } /* end switch */
  50.     } /* end while */
  51.     printf("The mean queue length seen by arriving customers is:%8.4f/n",((float) q_sum) / narr); 
  52.   
  53. /* end main */ 
  54. /**********************************************************************/
  55. int negexp(int mean) /* returns a negexp rv with mean ‘mean’ */
  56. {
  57. //Generate negative-exponentially distributed random numbers with linux C fuctions
  58.     return ( (int)(- log(drand48()) * mean + 0.5) );
  59. }
  60. /**********************************************************************/
  61. void arrival() /* a customer arrives */
  62. {
  63.     narr += 1; /* keep tally of number of arrivals */
  64.     q_sum += q;
  65.     schedule(negexp(iat), ARRIVAL); /* schedule the next arrival */
  66.     q += 1;
  67.     if (q == 1)
  68.         schedule(negexp(service_time), DEPARTURE);
  69. }
  70. /**********************************************************************/
  71. void departure() /* a customer departs */
  72. {
  73.     q -= 1;
  74.     if (q > 0) schedule(negexp(service_time), DEPARTURE);
  75. }
  76. /**********************************************************************/
  77. void schedule(int time_interval, int event)
  78. /* Schedules an event of type */
  79. /* ’event’ at time ’time_interval’ in the future */
  80. {
  81.     int event_time;
  82.     point x, t;
  83.     event_time = gmt + time_interval;
  84.     t = NEW(EVENTLIST);
  85.     for(x=head ; x->next->time<event_time && x->next!=tail ; x=x->next);
  86.     t->time = event_time;
  87.     t->event_type = event;
  88.     t->next = x->next;
  89.     x->next = t;
  90. }
  91. /**********************************************************************/
  92. int act() /* find the next event and go to it */
  93. {
  94.     int type;
  95.     point x;
  96.     gmt = head->next->time; /* step time forward to the next event */
  97.     type = head->next->event_type; /* Record type of this next event */
  98.     x = head->next; /* Delete event from linked list */
  99.     head->next = head->next->next;
  100.     free(x);
  101.     return type; /* return value is type of the next event */
  102. /**********************************************************************/
  103. void sim_init()
  104. /* initialise the simulation */
  105. {
  106.     printf("/nenter mean interarrival time and mean service time/n");
  107.     scanf("%d%d", &iat, &service_time);
  108.     printf("enter the total number of customers to be simulated/n");
  109.     scanf("%d", &total_events);
  110.     printf("enter the seed/n");
  111.     scanf("%ld", &seed);
  112.     srand48(seed);
  113.     head = NEW(EVENTLIST);
  114.     tail = NEW(EVENTLIST);
  115.     head->next = tail;
  116.     tail->next = tail;
  117.     q = 0;
  118.     narr = 0;
  119.     q_sum = 0;
  120.     schedule(negexp(iat), ARRIVAL); /* schedule the first arrival */
發佈了53 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章