Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4062402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:35:37+00:00 2026-05-20T15:35:37+00:00

How likely is it to get a HashCode collision with the function below in

  • 0

How likely is it to get a HashCode collision with the function below in following scenarios.

  1. With random int values for key[0],key[1], key[2], key[3]
  2. With random key values with the following constraints
    • key[0] <1,000,000
    • key[1] <10,000
    • key[2] <1,000
    • key[3] <1,000

Assume we have 10 Million objects.

int[] key=new int[4];    
public override int GetHashCode()
{
    // Use large prime multiples to create a unique hash key
    // Create the hash offsets using a "even powers of 2 minus 1" method, which gives 
    // primes most of the time.  
    int hashKey = 0;
    hashKey += 2047 * key[0];
    hashKey += 8191 * key[1];
    hashKey += 32767 * key[2];
    hashKey += 131071 * key[3];
    return hashKey;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:35:38+00:00Added an answer on May 20, 2026 at 3:35 pm

    I wrote a quick script to test this.

    import random
    
    def hash(key):
        hashKey = 0
        hashKey += 2047 * key[0]
        hashKey += 8191 * key[1]
        hashKey += 32767 * key[2]
        hashKey += 131071 * key[3]
        return hashKey
    
    seen = set()
    collisions = 0
    for i in range(0,10000000):
        x = hash([random.randint(0,1000000), random.randint(0,10000), random.randint(0,1000), random.randint(0,1000)])
        if x in seen:
            collisions += 1
        else:
            seen.add(x)
    
    print collisions
    

    When I ran it, it told me I got 23735 collisions.
    I also tried it on one million elements, and I got 247 collisions. Both numbers are averages over 4 runs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing app in which I made the mistake of using String.GetHashCode
I am getting a string hash like this: string content = a very long
I've recently started using LINQ quite a bit, and I haven't really seen any
I'm writing an IRC bot in Python, due to the alpha nature of it,
I don't want to know how... Just how complicated.... I'm thinking of securing a
I'm trying to identify what would be the best selling point for a set
Yes, I realize this is horrible UI and bad accessibility wise, but I am
I have lists of articles made of: title, subtitle and body. Now I need
Many of us need to deal with user input, search queries, and situations where
I have a project in Eclipse. When I run it from inside Eclipse, everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.