|
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
double EstWIP = 0; //estimated # of WIP in system
double EstVAR = 0; //variance
int Nrep; //number of replications
for (Nrep = 0; Nrep <= 200; Nrep++)
{
int y;
double sum =0;
int D = 1000000000;
int T = 0;
int X = 0;
srand((unsigned int)time(NULL));
int A = rand()%5+4;
for (T = 0; T<= 60; T)
{
if (D > A) // part arrives before departs
{
sum = sum + (A-T)*X; //total time(minutes) spend in system
T = A; //arrival time is new simulation time
X = X + 1;
y = rand()%5+4; //interarrival time
A = T + y; //next arrival time (update A)
if (X == 1) //If only 1 part in the system
{
y = rand()%3+2;
D = T + y; //next departure time (update D)
}
}
else // part departs before arrives
{
sum = sum + (D-T)*X;
T = D; //departure time is new simulatino time
X = X-1;
if (X > 0) //If more than 1 part in system
{
y = rand()%2+4;
D = T + y; //next departure time (update D)
}
else //no part in system
{
D = 1000000000; //big number since no parts in the system
}
}
EstWIP = EstWIP + sum/T;
EstVAR = EstVAR + pow(EstWIP-sum, 2);
cout <<"Estimate WIP is " <<EstWIP<<endl; //sometimes T is greater than 60 <---need to fix
cout <<"Estimated VAR is " <<EstVAR<<endl;
}
cout <<"================================================================"<<endl;
cout <<"Avg Estimated WIP is "<<EstWIP/Nrep<<" parts"<<endl;
cout <<"Avg Time an entity spends in system is "<<T/(EstWIP/Nrep)<<" minutes"<<endl;
double Mean = T/(EstWIP/Nrep);
cout <<"Mean is " <<Mean<<endl;
double Variance;
double Sigma;
Variance = EstVAR/Nrep;
Sigma = sqrt (Variance);
cout <<"Estimated Variance is "<<Variance<<endl;
cout <<"Estimated Standard Deviation is "<<Sigma<<endl;
}
system("pause");
return 0;
}
C++ 시작한지 얼마 안된 학생입니다.
Sigma = sqrt (Variance); 에서 저는 단순히 Variance 값에 루트를 씌우려는건데 더 큰 숫자가 나와버립니다ㅠ.
예를들어 Variance 값이 1.64 얼마였으면 sqrt 한 값은 더 작아야하는데 4059. 얼마가 나와버립니다. 제가 뭘 놓치고 있는걸까요?
|