0

I am trying to come up with a good hash function for a string and an integer that will just return a positive integer. I was thinking about taking the sum of the Unicode characters of the string and multiplying it with the integer, and then taking the absolute value of that. Would you say that is a good hash function? Will that give an even distribution? And do you have any ideas about how to test this with random or something like that?

This is what my class looks like now:

class MyHashableKey:

def __init__(self,int_value,string_value):
    self.int_value=int_value
    self.string_value=string_value

def __hash__(self):
    str_value = sum([ord(x) for x in self.string_value])
    my_hash = abs(str_value * self.int_value)
    return my_hash 
  • 3
    Any reason to not just return the hash of a tuple of the values? `def __hash__(self): return hash((self.string_value, self.int_value))` ? – Jon Clements Mar 13 '20 at 14:15
  • 1
    this is just a predictable hash there is no random creation – Hari Mar 13 '20 at 14:19
  • 1
    *Multiplying* by the integer would be a bad idea if the integer could potentially be zero - which would cause the string's value to become irrelevant to the hash. The usual way of combining elements into a hash is to XOR them. – jasonharper Mar 13 '20 at 14:55
  • Regarding suitability of a hash algorithm look at https://stackoverflow.com/questions/34595/what-is-a-good-hash-function and https://softwareengineering.stackexchange.com/questions/179618/how-to-test-if-a-hashing-algorithm-is-good – Mohammad Azim Mar 13 '20 at 15:08

0 Answers0