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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KDamon
    New Member
    • Oct 2017
    • 2

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

    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?

    Code:
    int generate_numbers(const int min, const int max)
    {
    LinkedSortedList<int> list;
    
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(min, max);
    
        list.insertSorted(dis(gen));    
    
        return dis(gen);
    }
    
    
    void InsertRandomInts() 
    {
    LinkedSortedList<int> list;
    LinkedSortedList<int> copyOfList(list);
    
    srand((unsigned)time(NULL));
    
    for (int i = 0; i < 50; ++i)
    {
        int b = generate_numbers(1, 100);
        list.insertSorted(b);
    
        if (b % 2) 
        {
            copyOfList.insertSorted(b);
            list.removeSorted(b);
        }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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 LinkedSortedLis t code look like?

    Comment

    • KDamon
      New Member
      • Oct 2017
      • 2

      #3
      I did test to see if they were there. I used this
      Code:
      cout << "The sorted list, without odd numbers, contains " << endl;
      	displayListForward(&list);
      	cout << "The sorted list, without odd numbers, in reverse, contains " << endl;
      	displayListBackwards(&list);
      	cout << "The sorted copy list contains " << endl;
      	displayListForward(&copyOfList);
      	cout << "The sorted copy list in reverse contains " << endl;
      	displayListBackwards(&copyOfList);
      
      	cout << "Copy list is cleared" << endl;
      	copyOfList.clear();
      	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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        Working...