-1

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by firstPerson
2

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.

Votes + Comments
While I understand his answer, the google sarcasm is not helpful. I understood hashcode, I just needed help fitting it into a closed array.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.