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

南笙酒味 提交于 2019-12-05 17:13:15

问题


the thing is that, the 1st number is already ORACLE LONG, second one a Date (SQL DATE, no timestamp info extra), the last one being a Short value in the range 1000-100'000.
how can I create sort of hash value that will be unique for each combination optimally?

string concatenation and converting to long later:
I don't want this, for example.

Day Month

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


回答1:


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

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

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

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

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

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

"BB".hashCode() == "Aa".hashCode() == 2122



回答2:


Your

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

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

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

00012 001 --> 00012001
00001 012 --> 00001012



回答3:


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)]


来源:https://stackoverflow.com/questions/1358468/how-to-create-unique-integer-number-from-3-different-integers-numbers1-oracle-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!