如果通过在Java中添加2个其他哈希代码生成新的哈希代码,那么冲突的概率是多少
例如:
Integer reportHashCode = reportFields.hashCode() + reportId.hashCode();
让我们假设Java的哈希码是32位,我们可以忽略哈希码本身的正常冲突。
答案 0 :(得分:2)
我会XOR
在这里而不是添加,因为xor的分布为1
和0
的50-50%。
答案 1 :(得分:1)
我们怎么发现?以下程序将为您模拟此项。请注意,sum的两个加数是随机生成的,因此两者都具有大约整数概率范围。实际上,您求和的两个哈希码可能在整个整数空间上没有平坦的分布。可以调整程序来模拟它。
package hashcode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class HashCode {
// Number of test cases
private static final int TEST_CASES = 10_000_000;
public static void main(String[] args) {
// Random number generator
Random rand = new Random();
// Map from integers (result hash codes) to a list of addend pairs that formed those hash codes
Map<Integer, Set<Pair>> hashCodesToComposites = new HashMap<>();
// Number of collissions
int collisions = 0;
// Running simulations
for (int i = 0; i < TEST_CASES; ++i) {
if (TEST_CASES / 4 == i) {
System.out.println("25 %");
}
if (TEST_CASES / 2 == i) {
System.out.println("50 %");
}
if ((TEST_CASES * 3) / 4 == i) {
System.out.println("75 %");
}
// Generating addends as random integers
int first = rand.nextInt();
int second = rand.nextInt();
// The pair; its hash code is the sum of the above
Pair pair = new Pair(first, second);
// Did it occur before?
if (hashCodesToComposites.containsKey(pair.hashCode())) {
// Getting the set of addend pairs that created this hash code
Set<Pair> composites = hashCodesToComposites.get(pair.hashCode());
// Checking if by any chance the two random numbers happened to be the same (almost negligible)
if (!composites.contains(pair)) {
// Actual collision from different numbers
collisions++;
// Adding to the set of composites
composites.add(pair);
} // Same numbers; doesn't count as collision
} else {
// First occurrence of this hash code
Set<Pair> composites = new HashSet<>();
composites.add(pair);
hashCodesToComposites.put(pair.hashCode(), composites);
}
}
// Results
System.out.println("Test cases: " + TEST_CASES);
System.out.println("Collisions: " + collisions);
System.out.println("Probability: " + ((double) collisions / (double) TEST_CASES));
}
private static class Pair {
final int first;
final int second;
final int hashCode;
Pair(int first, int second) {
this.first = first;
this.second = second;
hashCode = first + second;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
final Pair other = (Pair) obj;
return (this.first == other.first && this.second == other.second) || (this.first == other.second && this.second == other.first);
}
}
}
结果通常在0.00115左右。这意味着碰撞的概率大约为0.115%。我运行下面的代码来了解随机整数之间碰撞的几率。
package hashcode;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class HashCode2 {
// Number of test cases
private static final int TEST_CASES = 10_000_000;
public static void main(String[] args) {
// Random number generator
Random rand = new Random();
Set<Integer> hashCodes = new HashSet<>();
// Number of collissions
int collisions = 0;
// Running simulations
for (int i = 0; i < TEST_CASES; ++i) {
if (TEST_CASES / 4 == i) {
System.out.println("25 %");
}
if (TEST_CASES / 2 == i) {
System.out.println("50 %");
}
if ((TEST_CASES * 3) / 4 == i) {
System.out.println("75 %");
}
int next = rand.nextInt();
if (hashCodes.contains(next)) {
collisions++;
} else {
hashCodes.add(next);
}
}
// Results
System.out.println("Test cases: " + TEST_CASES);
System.out.println("Collisions: " + collisions);
System.out.println("Probability: " + ((double) collisions / (double) TEST_CASES));
}
}
概率实际上大致相同。它只是略低,但仍然是0.115%。最后,我再次尝试了第一个程序,但在Pair
的hashCode方法中使用xor而不是总和。结果?几乎是同样的事情。
因此,最后,如果两个哈希码和xor的哈希码具有良好的分布,则可以预期非常接近与两个哈希码和xor之和的随机整数相同的冲突率。 / p>