title: Designing a rehash function... how to avoid the same hash?
body: <p>So I'm working on a program that works with a hash table. Here's how it goes:</p>
  
  <p>1) Reads in a text file into a vector of "Symbol" objects (contains a name and a number)
  2) Hashes the name of the Symbol object. 
  3) Inserts this object into the hash table.</p>
  
  <p>So far I've been storing the uniquely generated hash keys as an array of integers. I then loop through the array and see if there's duplicates. </p>
  
  <p>If there are, I know there's a collision. This method has proven to be successful however, now I have to write a rehash() function so I can get a new key that won't result in a collision. But I can't figure out how to do this. </p>
  
  <p>I've included my Loop where I check the array of keys and my current hash function as well as my output. Any suggestions on where to go would be really appreciated. </p>
  
  <pre><code>for (int i=0; i &lt; TABLE_SIZE; i++)
          {
                if (i != j)
                {
                  if (array[i] == array[j])
                  {
                      cout &lt;&lt; endl;
                        cout &lt;&lt; "Collision occurred at" &lt;&lt; array[i] &lt;&lt; endl;
                      cout &lt;&lt; "Now rehashing..." &lt;&lt; endl;
                      // REHASH FUNCTION SHOULD GO HERE --&gt; rec.key = rehash(data);
                      cout &lt;&lt; "The new key is: " &lt;&lt; rec.key &lt;&lt; endl;
                      break;
                  }
                }
          }
  
          dataTable.insert(rec); //inserts the record object into the HashTable.
  </code></pre>
  
  <p>Existing Hash Function: </p>
  
  <pre><code>int hasher (string data)
    // POST: the index of entry is returned
    {       int sum = 0;
            for (int k = 0; k &lt; data.length(); k++)
                sum = sum + int(data[k]);
            return  sum % TABLE_SIZE;
    }
  </code></pre>
  
  <p>Output I'm Getting:</p>
  
  <p>count
        2
        The key for this Symbol object is: 7</p>
  
  <pre><code>  num
    2
    The key for this Symbol object is: 0
  
  
    Collision occurred at0
    Now rehashing...
    The new key is: 1
    myFloat
    4
    The key for this Symbol object is: 18
  
    myDouble
    5
    The key for this Symbol object is: 14
  
    name
    6
    The key for this Symbol object is: 18
  
  
    Collision occurred at18
    Now rehashing...
    The new key is: 1
    address
    6
    The key for this Symbol object is: 7
  
  
    Collision occurred at7
    Now rehashing...
    The new key is: 1
    salary
    5
    The key for this Symbol object is: 1
  
    gpa
    4
    The key for this Symbol object is: 18
  
  
    Collision occurred at18
    Now rehashing...
    The new key is: 1
    gdp
    5
    The key for this Symbol object is: 0
  
  
    Collision occurred at0
    Now rehashing...
    The new key is: 1
    pi
    5
    The key for this Symbol object is: 7
  
  
    Collision occurred at7
    Now rehashing...
    The new key is: 1
    city
    6
    The key for this Symbol object is: 0
  
  
    Collision occurred at0
    Now rehashing...
    The new key is: 1
    state
    6
    The key for this Symbol object is: 20
  
    county
    6
    The key for this Symbol object is: 2
  
    ch
    0
    The key for this Symbol object is: 14
  
  
    Collision occurred at14
    Now rehashing...
    The new key is: 1
    ch2
    0
    The key for this Symbol object is: 1
  
  
    Collision occurred at1
    Now rehashing...
    The new key is: 1
    ID
    1
    The key for this Symbol object is: 15
  
    studentID
    1
    The key for this Symbol object is: 13
  
    max
    3
    The key for this Symbol object is: 11
  
    max2
    3
    The key for this Symbol object is: 19
  
    greeting
    6
    The key for this Symbol object is: 13
  
  
    Collision occurred at13
    Now rehashing...
    The new key is: 1
    debt
    5
  </code></pre>
  
  <p>As you can see, when there is a collision it successfully detects it. Now I just need a way to rehash the key once again so that it doesn't happen in the future... because now the rehash is also a collision.</p>