Skip to content

Instantly share code, notes, and snippets.

@wenhuizhang
Created October 19, 2012 02:22
Show Gist options
  • Save wenhuizhang/3915909 to your computer and use it in GitHub Desktop.
Save wenhuizhang/3915909 to your computer and use it in GitHub Desktop.
Queing Theory :C
1.typedef double real;
2.#define then
3.
4.#define A 16807L /* multiplier (7**5) for 'ranf' */
5.#define M 2147483647L /* modulus (2**31-1) for 'ranf' */
6.
7.static long In[16]= {0L, /* seeds for streams 1 thru 15 */
8. 1973272912L, 747177549L, 20464843L, 640830765L, 1098742207L,
9. 78126602L, 84743774L, 831312807L, 124667236L, 1172177002L,
10. 1124933064L, 1223960546L, 1878892440L, 1449793615L, 553303732L};
11.
12.static int strm=1; /* index of current stream */
13.
14.#if CPU==8086
15./*------------- UNIFORM [0, 1] RANDOM NUMBER GENERATOR -------------*/
16./* */
17./* This implementation is for Intel 8086/8 and 80286/386 CPUs using */
18./* C compilers with 16-bit short integers and 32-bit long integers. */
21.real ranf()
22. {
23. short *p,*q,k; long Hi,Lo;
24. /* generate product using double precision simulation (comments */
25. /* refer to In's lower 16 bits as "L", its upper 16 bits as "H") */
26. p=(short *)&In[strm]; Hi=*(p+1)*A; /* 16807*H->Hi */
27. *(p+1)=0; Lo=In[strm]*A; /* 16807*L->Lo */
28. p=(short *)&Lo; Hi+=*(p+1); /* add high-order bits of Lo to Hi */
29. q=(short *)&Hi; /* low-order bits of Hi->LO */
30. *(p+1)=*q&0X7FFF; /* clear sign bit */
31. k=*(q+1)<<1; if (*q&0X8000) then k++; /* Hi bits 31-45->K */
32. /* form Z + K [- M] (where Z=Lo): presubtract M to avoid overflow */
33. Lo-=M; Lo+=k; if (Lo<0) then Lo+=M;
34. In[strm]=Lo;
35. return((real)Lo*4.656612875E-10); /* Lo x 1/(2**31-1) */
36. }
37.#endif
39.#if CPU==68000
40./*------------- UNIFORM [0, 1] RANDOM NUMBER GENERATOR -------------*/
41./* */
42./* This implementation is for Motorola 680x0 CPUs using C compilers */
43./* with 16-bit short integers and 32-bit long integers. */
44./* */
45./*--------------------------------------------------------------------*/
46.real ranf()
47. {
48. short *p,*q,k; long Hi,Lo;
49. /* generate product using double precision simulation (comments */
50. /* refer to In's lower 16 bits as "L", its upper 16 bits as "H") */
51. p=(short *)&In[strm]; Hi= *(p)*A; /* 16807*H->Hi */
52. *(p)=0; Lo=In[strm]*A; /* 16807*L->Lo */
53. p=(short *)&Lo; Hi+= *(p); /* add high-order bits of Lo to Hi */
54. q=(short *)&Hi; /* low-order bits of Hi->LO */
55. *(p)= *(q+1)&0X7FFF; /* clear sign bit */
56. k= *(q)<<1; if (*(q+1)&0X8000) then k++; /* Hi bits 31-45->K */
57. /* form Z + K [- M] (where Z=Lo): presubtract M to avoid overflow */
58. Lo-=M; Lo+=k; if (Lo<0) then Lo+=M;
59. In[strm]=Lo;
60. return((real)Lo*4.656612875E-10); /* Lo x 1/(2**31-1) */
61. }
62.#endif
63.
64./*-------------------- SELECT GENERATOR STREAM ---------------------*/
65.stream(n)
66. int n;
67. { /* set stream for 1<=n<=15, return stream for n=0 */
68. if ((n<0)||(n>15)) then error(0,"stream Argument Error");
69. if (n) then strm=n;
70. return(strm);
71. }
72.
73./*-------------------------- SET/GET SEED --------------------------*/
74.long seed(Ik,n)
75. long Ik; int n;
76. { /* set seed of stream n for Ik>0, return current seed for Ik=0 */
77. if ((n<1)||(n>15)) then error(0,"seed Argument Error");
78. if (Ik>0L) then In[n]=Ik;
79. return(In[n]);
80. }
81.
82./*------------ UNIFORM [a, b] RANDOM VARIATE GENERATOR -------------*/
83.real uniform(a,b)
84. real a,b;
85. { /* 'uniform' returns a psuedo-random variate from a uniform */
86. /* distribution with lower bound a and upper bound b. */
87. if (a>b) then error(0,"uniform Argument Error: a > b");
88. return(a+(b-a)*ranf());
89. }
90.
91./*-------------------- RANDOM INTEGER GENERATOR --------------------*/
92.random(i,n)
93. int i,n;
94. { /* 'random' returns an integer equiprobably selected from the */
95. /* set of integers i, i+1, i+2, . . , n. */
96. if (i>n) then error(0,"random Argument Error: i > n");
97. n-=i; n=(n+1.0)*ranf();
98. return(i+n);
99. }
101./*-------------- EXPONENTIAL RANDOM VARIATE GENERATOR --------------*/
102.real expntl(x)
103. real x;
104. { /* 'expntl' returns a psuedo-random variate from a negative */
105. /* exponential distribution with mean x. */
106. return(-x*log(ranf()));
107. }
108.
109./*---------------- ERLANG RANDOM VARIATE GENERATOR -----------------*/
110.real erlang(x,s)
111. real x,s;
112. { /* 'erlang' returns a psuedo-random variate from an erlang */
113. /* distribution with mean x and standard deviation s. */
114. int i,k; real z;
115. if (s>x) then error(0,"erlang Argument Error: s > x");
116. z=x/s; k=(int)z*z;
117. z=1.0; for (i=0; i<K; s s, deviation * standard and x mean with distribution hyperexponential stage two- Morse?s from variate psuedo-random a returns ?hyperx? { x,s; real hyperx(x,s) -----------* GENERATION VARIATE RANDOM HYPEREXPONENTIAL *----------- } k)*log(z)); return(-(x z*="ranf();" i++)>x. */
118. real cv,z,p;
119. if (s<=x) then error(0,"hyperx Argument Error: s not > x");
120. cv=s/x; z=cv*cv; p=0.5*(1.0-sqrt((z-1.0)/(z+1.0)));
121. z=(ranf()>p)? (x/(1.0-p)):(x/p);
122. return(-0.5*z*log(ranf()));
123. }
124.
125./*----------------- NORMAL RANDOM VARIATE GENERATOR ----------------*/
126.real normal(x,s)
127. real x,s;
128. { /* 'normal' returns a psuedo-random variate from a normal dis- */
129. /* tribution with mean x and standard deviation s. */
130. real v1,v2,w,z1; static real z2=0.0;
131. if (z2!=0.0)
132. then {z1=z2; z2=0.0;} /* use value from previous call */
133. else
134. {
135. do
136. {v1=2.0*ranf()-1.0; v2=2.0*ranf()-1.0; w=v1*v1+v2*v2;}
137. while (w>=1.0);
138. w=sqrt((-2.0*log(w))/w); z1=v1*w; z2=v2*w;
139. }
140. return(x+z1*s);
141. }
1.typedef double real;
2.#define then
3.
4.#define A 16807L /* multiplier (7**5) for 'ranf' */
5.#define M 2147483647L /* modulus (2**31-1) for 'ranf' */
6.
7.static long In[16]= {0L, /* seeds for streams 1 thru 15 */
8. 1973272912L, 747177549L, 20464843L, 640830765L, 1098742207L,
9. 78126602L, 84743774L, 831312807L, 124667236L, 1172177002L,
10. 1124933064L, 1223960546L, 1878892440L, 1449793615L, 553303732L};
11.
12.static int strm=1; /* index of current stream */
13.
14.#if CPU==8086
15./*------------- UNIFORM [0, 1] RANDOM NUMBER GENERATOR -------------*/
16./* */
17./* This implementation is for Intel 8086/8 and 80286/386 CPUs using */
18./* C compilers with 16-bit short integers and 32-bit long integers. */
21.real ranf()
22. {
23. short *p,*q,k; long Hi,Lo;
24. /* generate product using double precision simulation (comments */
25. /* refer to In's lower 16 bits as "L", its upper 16 bits as "H") */
26. p=(short *)&In[strm]; Hi=*(p+1)*A; /* 16807*H->Hi */
27. *(p+1)=0; Lo=In[strm]*A; /* 16807*L->Lo */
28. p=(short *)&Lo; Hi+=*(p+1); /* add high-order bits of Lo to Hi */
29. q=(short *)&Hi; /* low-order bits of Hi->LO */
30. *(p+1)=*q&0X7FFF; /* clear sign bit */
31. k=*(q+1)<<1; if (*q&0X8000) then k++; /* Hi bits 31-45->K */
32. /* form Z + K [- M] (where Z=Lo): presubtract M to avoid overflow */
33. Lo-=M; Lo+=k; if (Lo<0) then Lo+=M;
34. In[strm]=Lo;
35. return((real)Lo*4.656612875E-10); /* Lo x 1/(2**31-1) */
36. }
37.#endif
39.#if CPU==68000
40./*------------- UNIFORM [0, 1] RANDOM NUMBER GENERATOR -------------*/
41./* */
42./* This implementation is for Motorola 680x0 CPUs using C compilers */
43./* with 16-bit short integers and 32-bit long integers. */
44./* */
45./*--------------------------------------------------------------------*/
46.real ranf()
47. {
48. short *p,*q,k; long Hi,Lo;
49. /* generate product using double precision simulation (comments */
50. /* refer to In's lower 16 bits as "L", its upper 16 bits as "H") */
51. p=(short *)&In[strm]; Hi= *(p)*A; /* 16807*H->Hi */
52. *(p)=0; Lo=In[strm]*A; /* 16807*L->Lo */
53. p=(short *)&Lo; Hi+= *(p); /* add high-order bits of Lo to Hi */
54. q=(short *)&Hi; /* low-order bits of Hi->LO */
55. *(p)= *(q+1)&0X7FFF; /* clear sign bit */
56. k= *(q)<<1; if (*(q+1)&0X8000) then k++; /* Hi bits 31-45->K */
57. /* form Z + K [- M] (where Z=Lo): presubtract M to avoid overflow */
58. Lo-=M; Lo+=k; if (Lo<0) then Lo+=M;
59. In[strm]=Lo;
60. return((real)Lo*4.656612875E-10); /* Lo x 1/(2**31-1) */
61. }
62.#endif
63.
64./*-------------------- SELECT GENERATOR STREAM ---------------------*/
65.stream(n)
66. int n;
67. { /* set stream for 1<=n<=15, return stream for n=0 */
68. if ((n<0)||(n>15)) then error(0,"stream Argument Error");
69. if (n) then strm=n;
70. return(strm);
71. }
72.
73./*-------------------------- SET/GET SEED --------------------------*/
74.long seed(Ik,n)
75. long Ik; int n;
76. { /* set seed of stream n for Ik>0, return current seed for Ik=0 */
77. if ((n<1)||(n>15)) then error(0,"seed Argument Error");
78. if (Ik>0L) then In[n]=Ik;
79. return(In[n]);
80. }
81.
82./*------------ UNIFORM [a, b] RANDOM VARIATE GENERATOR -------------*/
83.real uniform(a,b)
84. real a,b;
85. { /* 'uniform' returns a psuedo-random variate from a uniform */
86. /* distribution with lower bound a and upper bound b. */
87. if (a>b) then error(0,"uniform Argument Error: a > b");
88. return(a+(b-a)*ranf());
89. }
90.
91./*-------------------- RANDOM INTEGER GENERATOR --------------------*/
92.random(i,n)
93. int i,n;
94. { /* 'random' returns an integer equiprobably selected from the */
95. /* set of integers i, i+1, i+2, . . , n. */
96. if (i>n) then error(0,"random Argument Error: i > n");
97. n-=i; n=(n+1.0)*ranf();
98. return(i+n);
99. }
101./*-------------- EXPONENTIAL RANDOM VARIATE GENERATOR --------------*/
102.real expntl(x)
103. real x;
104. { /* 'expntl' returns a psuedo-random variate from a negative */
105. /* exponential distribution with mean x. */
106. return(-x*log(ranf()));
107. }
108.
109./*---------------- ERLANG RANDOM VARIATE GENERATOR -----------------*/
110.real erlang(x,s)
111. real x,s;
112. { /* 'erlang' returns a psuedo-random variate from an erlang */
113. /* distribution with mean x and standard deviation s. */
114. int i,k; real z;
115. if (s>x) then error(0,"erlang Argument Error: s > x");
116. z=x/s; k=(int)z*z;
117. z=1.0; for (i=0; i<K; s s, deviation * standard and x mean with distribution hyperexponential stage two- Morse?s from variate psuedo-random a returns ?hyperx? { x,s; real hyperx(x,s) -----------* GENERATION VARIATE RANDOM HYPEREXPONENTIAL *----------- } k)*log(z)); return(-(x z*="ranf();" i++)>x. */
118. real cv,z,p;
119. if (s<=x) then error(0,"hyperx Argument Error: s not > x");
120. cv=s/x; z=cv*cv; p=0.5*(1.0-sqrt((z-1.0)/(z+1.0)));
121. z=(ranf()>p)? (x/(1.0-p)):(x/p);
122. return(-0.5*z*log(ranf()));
123. }
124.
125./*----------------- NORMAL RANDOM VARIATE GENERATOR ----------------*/
126.real normal(x,s)
127. real x,s;
128. { /* 'normal' returns a psuedo-random variate from a normal dis- */
129. /* tribution with mean x and standard deviation s. */
130. real v1,v2,w,z1; static real z2=0.0;
131. if (z2!=0.0)
132. then {z1=z2; z2=0.0;} /* use value from previous call */
133. else
134. {
135. do
136. {v1=2.0*ranf()-1.0; v2=2.0*ranf()-1.0; w=v1*v1+v2*v2;}
137. while (w>=1.0);
138. w=sqrt((-2.0*log(w))/w); z1=v1*w; z2=v2*w;
139. }
140. return(x+z1*s);
141. }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment