954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Even Hash Distribution

This is a homework question
I am attempting to write a hash function that will evenly distribute 20 randomly selected census name into 20 buckets. I may have up to three collisions of two each.

public int hashCode(Contact c) {
		// TODO Auto-generated method stub
		int result = 0;
		for(char d: c.getFirstName().toCharArray()) {
			result += d % 26;
		}
		result *= c.getFirstName().charAt(0) / c.getLastName().charAt(0) % 26;
		
		for(char d: c.getLastName().toCharArray()) {
			result += d % 26;
		}
		result *= c.getFirstName().length();
		result /= c.getLastName().length();
		
		//result *= c.getLastName().charAt(c.getLastName().length() / 2) % 26;
		
		return result % 20;
	}

This code is a bit random and when ran produces a bell curve. I understand the English language is a bit biased towards vowels and certain characters. Any ideas on how to more evenly distribute the names.

tylerjgarland
Newbie Poster
7 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Try using this to hash strings:

unsigned long hash(unsigned char *str){
        unsigned long hash = 5381;
        int c;

        while (c = *str++)
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

        return hash;
    }


Then you can incorporate this for the person's last and first name. The algorithm above was found in google.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: