views:

153

answers:

5

i'm not sure why i cant get diff values for my variables here, help!

int main()
{
    srand(time(NULL));
    srand48(time(NULL));
    Packet* firstPacket = new Packet();
    firstPacket->packetSize =  randSize();
    firstPacket->dest = randDest();
    firstPacket->arrivalTime = myExp(lamb);
    Host[firstPacket->dest].Frame.push_back(firstPacket); // adding first packet
    Host[firstPacket->dest].numOfPack++;
    calcFrameSize(Host[firstPacket->dest]);
    cout << Host[token].frameTVTime << " " << Host[token].frameSize
        << " " << token << " " << curTime << endl;
}
+3  A: 

What is in randSize() and randDest()? Make sure that those functions don't call srand() inside.

Igor Krivokon
+1: Given approximately no information, this is the best guess I can imagine.
ojrac
A: 

this is what i have for the other functions.

inline int randSize() { return (rand()%1518 + 64); }

inline int randDest() { return (rand()%10); }

inline float time(int size) { return (10.0*(float)size/12500); }

and this is what i have in the console:

~/152a/2phase$ ./phase2
0 0 1 0.01
0.5752 719 6 0.06

i get those numbers consistently, which i dont think i should be.

thanks.

aZn137
i always get the same values for the packetSize, packetDestination and its travel time.
aZn137
Why do you define time()? I don't see where it is used in your code. The name of the function is bad, since there's a system function called time(), and this is what you should call in srand(). If you really need your own time, rename it to something else.
Igor Krivokon
+5  A: 

Are your statements:

srand(time(NULL));
srand48(time(NULL));

calling your:

inline float time(int size) { return (10.0*(float)size/12500); }

rather than the system time?

That would explain why you are not getting random numbers.

As pointed out time() is a bad name for that function.

Duck
A: 
srand(time(0));

or

#include <sys/time.h>
timeval tim;
gettimeofday(&tim, NULL);
srand((unsigned int) tim.tv_usec);
A: 

renaming my time() function worked. thanks a bunch guys.

aZn137