0

Is it possible to create a custom hash algorithm, something that can provide an integer within a specific range. Example below.

(a,b) is the input to the hash. (a,b) != (b,a) where a and b are both integers >= 0

The solution must be within the range (min,max).

Would this be possible? With this I would like (a,b) being hashed on two occasions to provide the same integer if also being given the same range as well.

Thank you.

4
  • 2
    Yes, of course it would be possible. Why don't you try writing one? Commented Nov 5, 2017 at 22:11
  • You could start from here: hashCode() and to get numbers between a specific range you can use modular arithmetic Commented Nov 5, 2017 at 22:17
  • It's impossible to guarantee that (a,b) != (b,a). Hashes aren't collision-proof. Commented Nov 5, 2017 at 22:43
  • @shmosel It's absolutely possible to guarantee that, whenever a != b. Consider the function (a,b) -> ( a > b ? 1 : 0 ) Commented Nov 6, 2017 at 1:58

1 Answer 1

1

(a,b) is the input to the hash. (a,b) != (b,a) where a and b are both integers >= 0

No. Suppose a = b, then (a,b) = (b,a). This constraint violates consistency of hashing therefore impossible

Sign up to request clarification or add additional context in comments.

2 Comments

That is a good point. What if we assumed that (a,b) cannot equal (b,a) except for in the case that they were equal? Or would assume that was the case in that one situation make it impossible as well?
@Jamie Then it is possible. But it will be hard to find a good one if (a, b) != (a, b) for all a, b. The easiest solution would be simply a - b. But this isn't a good one because it may not be uniformly distributed if a and b have some pattern. To address the min, max range issue is rather easy. Just transfrom your hash h into min + h % p, where p is some prime no greater than (max - min)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.