By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

How Do I Generate A Linked List of Unique Random Ints?

P: 2
I made a linked sorted list type, and I'm trying to insert unique random integers into it. I tried using the following method to keep the integers unique, but when I try it, I'm still getting duplicate integers. Is there something I'm missing with this method?

Expand|Select|Wrap|Line Numbers
  2. int generate_numbers(const int min, const int max)
  3. {
  4. LinkedSortedList<int> list;
  6. std::random_device rd;
  7. std::mt19937 gen(rd());
  8. std::uniform_int_distribution<> dis(min, max);
  10.     list.insertSorted(dis(gen));    
  12.     return dis(gen);
  13. }
  16. void InsertRandomInts() 
  17. {
  18. LinkedSortedList<int> list;
  19. LinkedSortedList<int> copyOfList(list);
  21. srand((unsigned)time(NULL));
  23. for (int i = 0; i < 50; ++i)
  24. {
  25.     int b = generate_numbers(1, 100);
  26.     list.insertSorted(b);
  28.     if (b % 2) 
  29.     {
  30.         copyOfList.insertSorted(b);
  31.         list.removeSorted(b);
  32.     }
  33. }
Oct 29 '17 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
I'm not sure I follow the logic exactly. Once you generate an int you insert it into the sorted list without ever testing to see if the int is already there.

Then after that you test the generated int for odd/even. If odd you remove the int from the sorted list and add it to the copy without test to see if the int is already there. Then you remove it from the sorted list.

If the generated int was even you just leave in the sorted list.

Why all this processing?

You have a sorted list.
You generate a value to add.
Read the list for the value.
If the value is there, discard the new value.
If the value is not there, add it to the list.

What does the LinkedSortedList code look like?
Oct 29 '17 #2

P: 2
I did test to see if they were there. I used this
Expand|Select|Wrap|Line Numbers
  1. cout << "The sorted list, without odd numbers, contains " << endl;
  2.     displayListForward(&list);
  3.     cout << "The sorted list, without odd numbers, in reverse, contains " << endl;
  4.     displayListBackwards(&list);
  5.     cout << "The sorted copy list contains " << endl;
  6.     displayListForward(&copyOfList);
  7.     cout << "The sorted copy list in reverse contains " << endl;
  8.     displayListBackwards(&copyOfList);
  10.     cout << "Copy list is cleared" << endl;
  11.     copyOfList.clear();
  12.     displayListForward(&copyOfList);

I want to move the odd numbers into copy list and clear it. It's my way of clearing the odd numbers out of the first list. All I want to know is how to keep from getting repeated numbers in my list.
Oct 29 '17 #3

Expert Mod 5K+
P: 9,197
There shouldn't be odd numbers in the first list to begin with.

Make your test for odd/even first.
Then , if an odd value, add it to the copy
Otherwise, add it to the first list.

And by "add", I mean that you write a function that scans the list and inserts the value if snot there. By "insert" I mean the value is inserted at the place such that the sort sequence is maintained.

BTW displaying the list is not the same a checking for an existing value.

Keep posting.
Oct 30 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.