Board index » cppbuilder » Queues & Random Numbers

Queues & Random Numbers


2003-06-29 11:57:21 PM
cppbuilder98
I'm not sure what exactly I'm doing wrong here, please advise.
I'm trying to randomly select numbers from the interval [0, 3]
If the number is either 0 or 3, I will ignore it, otherwise I will
enqueue that many customers at the time of arrival.
For every minute that passes I will take one serve one customer,
thereby removing them from the queue.
The problem I'm facing with the current implementation is that
the program does not accurately track the customers upon
arrival. Additionally, it crashes for any for intervals less than
four minutes (simLength = 4).
Thanks in Advance,
Andrew
========================================
for ( minute = 0 ; minute < simLength ; minute++ )
{
// Dequeue the first customer in line (if any). Increment
// totalServed, add the time that this customer waited to
// totalWait, and update maxWait if this customer waited
// longer than any previous customer.
if (!custQ.isEmpty())
{
timeArrived = custQ.dequeue();
waitTime = minute - timeArrived;
totalServed++;
totalWait += waitTime;
if (waitTime>maxWait)
maxWait = waitTime;
}
// Determine the number of new customers and add them to
// the line.
const int N = 4;
srand((unsigned)time(NULL)+minute);
numArrivals = (rand() % N);
switch(numArrivals)
{
case 0:
case 3: break;
case 1: custQ.enqueue(minute); break;
case 2: custQ.enqueue(minute); custQ.enqueue(minute); break;
}
}
 
 

Re:Queues & Random Numbers

Andrew Edwards wrote:
Quote
for ( minute = 0 ; minute < simLength ; minute++ )
{
[...]
const int N = 4;
srand((unsigned)time(NULL)+minute);
numArrivals = (rand() % N);
This way of calling srand() and rand() is completely broken. You should
only ever call srand() once, in the whole program, not once per
iteration of some loop. _Certainly_ not once per call of rand().
Whatever other errors there are in your code's behaviour almost
certainly have nothing to do with the fragment you showed, nor with
randum number generation. My prime suspect would be bugs in your
queueing code.
--
Hans-Bernhard Broeker ( XXXX@XXXXX.COM )
Even if all the snow were burnt, ashes would remain.
 

Re:Queues & Random Numbers

Thanks! :-)
I found the culprit; A devide by zero error on the last line of main!
Sorry about the confusion!
Andrew
 

{smallsort}

Re:Queues & Random Numbers

"Andrew Edwards" < XXXX@XXXXX.COM >wrote in message
Quote
// Dequeue the first customer in line (if any). Increment
[..snip..]
if (!custQ.isEmpty())
{
timeArrived = custQ.dequeue();
[..snip..]
Glad you found your divide by zero error.
Here's some design related feedback.
- You may wish to think about the name 'dequeue'. It already has an
established meaning - double ended queue - and there is a standard library
component with a similar name (std::deque). (It's like a double-ended
vector.)
- I'd expect the result of removing something from the front of the queue to
be a customer object, not a time. (I'd expect the time waited by that
customer to be a property I'd ask that object for.)
Hth,
Fazl