I understand, using srand(time(0)), helps in setting the random seed. However, the following code, stores the same set of numbers for two different lists.
Wondering, how do I generate the different set of numbers when the following function gets called more than once.
void storeRandomNos(std::list<int>& dataToStore)
{
int noofElements = 0;
srand(time(0));
noofElements = (rand() % 14 ) + 1;
while ( noofElements --)
{
dataToStore.push_back(rand() % 20 + 1 );
}
}
Here is the rest of the code.
void printList(const std::list<int>& dataElements, const char* msg);
void storeRandomNos(std::list<int>& dataToStore);
int main()
{
std::list<int> numberColl1;
std::list<int> numberColl2;
storeRandomNos(numberColl1);
storeRandomNos(numberColl2);
printList(numberColl1 , "List1");
printList(numberColl2 , "Second list");
}
void printList(const std::list<int>& dataElements, const char* msg)
{
std::cout << msg << std::endl;
std::list<int>::const_iterator curLoc = dataElements.begin();
for ( ; curLoc != dataElements.end() ; ++curLoc)
{
std::cout << *curLoc << ' ';
}
}