Hello, I am doing a homework project in which I have to model the calls coming into a call center. The arrival rate is an exponentially distributed random variable, Y, and the service length is an exponentially distributed random variable, X.
I have the program running successfully if the calls all arrive in exactly Y mins, and are serviced for exactly X mins. However, when I try and implement the software using a RNG I always get the same random number over and over.
Here is the function for determining the length of time till the next call, or length of service time.
float expRand(double y) //y is the mean value for the exponentially
{ //distributed RV (Y or U)
float u; // uniformly distributed random number
float x; // exponentially distributed random number
u=rand()/RAND_MAX; // ensure u is on the interval [0,1)
x=log(1-u)/(-y); //convert to exponential distribution
return x;
}
This always gives me the same return value every time the function is called. An example of what my function calls look like is
Tev=tsim+expRand(U);
where the purpose of this expression is to take Tev (the next time a call will finish being serviced) and set it equal to the current simulation time plus a random amount of time that the call should last for.
An expression like the one above is evaluated multiple times, however, the value for Tev is the same every time, and I want it to be different by the random amount generated by expRand(U).
The problem is that C++ creates pseudo-random numbers. If you seed the generator you can get different sequences but with the same seed there will be the same sequence of random numbers.
Seed like this:
1 2
void srand ( unsignedint seed );
srand(N);
If you need to have randomly generated seeds for example you can use
1 2 3 4
#include <time.h>
time_t seconds;
seconds = time (NULL);
srand(seconds);
as a seed generator that always seeds with different value. (I use to do that at least). It's not really random but it gives the experience of giving different value every time you execute the program.
I'm not sure where you would like me to place the prototype and the srand(N) command.
I placed the function prototype in the beginning of my .cpp file.
srand(1000) is the first statement in expRand() above.
I still have the same error as before though.
While debugging and stepping through the code, I found the following: after execution of
u=rand()/RAND_MAX;
u=0 every time.
However, when I remove /RAND_MAX and use
u=rand();
I do get a number that changes depending on the seed value. So I guess my new question is, how should I generate a uniformly distributed random variable on the interval [0,1)