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.

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.

commented: While I understand his answer, the google sarcasm is not helpful. I understood hashcode, I just needed help fitting it into a closed array. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.