[ACCEPTED]-how to create unique integer number from 3 different integers numbers(1 Oracle Long, 1 Date Field, 1 Short)-numbers

Accepted answer
Score: 17

When you have a few numeric values and need 19 to have a single "unique" (that is, statistically 18 improbable duplicate) value out of them 17 you can usually use a formula like:

h = (a*P1 + b)*P2 + c

where 16 P1 and P2 are either well-chosen numbers 15 (e.g. if you know 'a' is always in the 1-31 14 range, you can use P1=32) or, when you know 13 nothing particular about the allowable ranges 12 of a,b,c best approach is to have P1 and 11 P2 as big prime numbers (they have the least 10 chance to generate values that collide). For 9 an optimal solution the math is a bit more 8 complex than that, but using prime numbers 7 you can usually have a decent solution.

For 6 example, Java implementation for .hashCode() for an 5 array (or a String) is something like:

h = 0;
for (int i = 0; i < a.length; ++i)
    h = h * 31 + a[i];

Even 4 though personally, I would have chosen a 3 prime bigger than 31 as values inside a 2 String can easily collide, since a delta 1 of 31 places can be quite common, e.g.:

"BB".hashCode() == "Aa".hashCode() == 2122
Score: 4

Your

12 1  --> 121
1 12  --> 121

problem is easily fixed by zero-padding 5 your input numbers to the maximum width 4 expected for each input field.

For example, if 3 the first field can range from 0 to 10000 2 and the second field can range from 0 to 1 100, your example becomes:

00012 001 --> 00012001
00001 012 --> 00001012
Score: 0

In python, you can use this:

#pip install pairing
import pairing as pf
n = [12,6,20,19]
print(n)
key = pf.pair(pf.pair(n[0],n[1]),
              pf.pair(n[2], n[3]))
print(key)
m = [pf.depair(pf.depair(key)[0]),
     pf.depair(pf.depair(key)[1])]
print(m)

Output is:

[12, 6, 20, 19]
477575
[(12, 6), (20, 19)]

0

More Related questions