C ++ not randomization

I'm not sure why I can't 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;
}

      

0


a source to share


4 answers


Are your statements:

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

      

call:

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

      



not the system time?

This explains why you are not getting random numbers.

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

+5


a source


What's in randSize () and randDest ()? Make sure these functions don't call srand () internally.



+3


a source


srand(time(0));

      

or

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

      

+1


a source


this is what I have for 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 am getting these numbers sequentially and I don't think there should be.

thanks.

0


a source







All Articles