3

Given 2 integers a and b (positive or negative). Is there any formula / method for generating unique ID number?

note: 1. result from f(a,b) and f(b,a) should be different. 2. calculating f(a,b) for x times (x > 1), the result should be same.

To make clear about the question, this function f(n) = (n * p) % q (where n=input sequence value, p=step size, q=maximum result size, n=non-negative integer, n < q, p < q, p ⊥ q (coprime)) will give unique ID number.

But, in my requirement, input are two numbers, a and b can be negative or positive integer.

any reference is appreciable

stranger
  • 159
  • 1
  • 3
  • 15
  • is there a known maximum upper and lower limit of these integers? – Spacedman Mar 21 '15 at 21:03
  • @Spacedman: thanks Spacedman for fast response, there is no limit for this integers.. – stranger Mar 21 '15 at 21:10
  • In practice -- when working with computers -- there's always an upper limit. – davidhigh Mar 21 '15 at 21:20
  • And the output "number" has to be an integer too? No decimals? – Spacedman Mar 21 '15 at 21:20
  • If the numbers really are unbounded then this is a number theory question and should prob be on the maths stackexchange site. And then I think the answer is "cant be done" (because you are mapping a 2d space to a 1d space). – Spacedman Mar 21 '15 at 21:23
  • @davidhigh : yup.. I know that.. but what I mean var a and b in this case is not really big.. maybe -1000 until 1000 --Spacedman : yes, I wish it would be better if the result is integer, but it's ok if not integer.. maybe I can get something for the idea – stranger Mar 21 '15 at 21:27
  • @Spacedman: my bad.. I dunno if there is exist math stackoverflow.. ok.. I will try to move this question – stranger Mar 21 '15 at 21:29
  • have you considered my answer, yet? It's one possibility to do what you request -- but in fact, you could use any isomorphism from Z x Z -> Z (here Z means the set of integer numbers). – davidhigh Mar 21 '15 at 23:05
  • Your question is too vague. What do you mean generating a unique id? You can create an auto incremented id generation but if you create random numbers with a and b you have to check if that value exists, otherwise it is not unique. Please explain in more detail. – Nookie Mar 21 '15 at 21:01
  • Possible duplicate of [Mapping two integers to one, in a unique and deterministic way](http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way) – Aramis7d Mar 27 '17 at 04:47

2 Answers2

2

You could generate a long (64 bit) from 2 integers (32 bit) by just right bit shifting the first integer with 32 and then add the second integer.

private long uniqueId(int left, int right) {
    long uniqueId = (long) left;
    uniqueId = uniqueId <<< 32;
    uniqueId += (long) right;
    return uniqueId;
}
Stefaan Neyts
  • 1,814
  • 1
  • 14
  • 22
1

Say your integers have a range in [MIN_INT,MAX_INT]. Then, given an integer n from this range, the function

f(n) = n - MIN_INT

attributes a unique positive integer f(n) in the range [0, MAX_INT - MIN_INT], which is often called a rank.

Denote M = MAX_INT - MIN_INT + 1. Then, to find a unique id g(n,m) of two concatenated integers n and m, you can use the common access style also used for two-dimensional arrays:

g(n,m) = f(n)*M + f(m)

That is, you simply offset the second integer by the largest possible value and count on.

Practically, of course, you have to be careful in order to avoid overflows -- that is, you should use some suited data types.


Here is an example: say your integers come from the range [-1,4], thus M=6. Then, for two integers n=3 and m=-1 out of this range, g(n,m) = 3*6 + 0 = 18 can be used as id.

davidhigh
  • 12,239
  • 1
  • 34
  • 64