Advertisement
bobmarley12345

2.6M/16M collisions for X,Y pair

Jan 11th, 2023
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. /**
  2.  * Calculate a hash code for a pair of coordinates
  3.  * @return The hashcode
  4.  * @author REghZy
  5.  */
  6. public static int hash(int x, int y) {
  7.     // 15.8M/16M collisions
  8.     //   return this.hash = (this.x * 31) ^ (68 + this.y);
  9.     // 15.3M/16M collisions
  10.     //   return this.hash = (this.x * 0x1f1f1f1f) ^ (this.y);
  11.     // 8000/16M collisions
  12.     // int hash = 'X';
  13.     // hash = (31 * hash) + ':';
  14.     // hash = aggregateHash(hash, Integer.toString(x));
  15.     // hash = (31 * hash) + 'Y';
  16.     // hash = (31 * hash) + ':';
  17.     // hash = aggregateHash(hash, Integer.toString(y));
  18.     // return hash;
  19.     // return new StringBuilder(24).append("X:").append(x).append("Y:").append(y).toString().hashCode();
  20.     // 8M/16M collisions
  21.     //   return (x * 0x101DEAD) ^ (y);
  22.     // 2.6M/16M collisions
  23.     return (x * 0x101DEAD) ^ (y * 0xBEEF);
  24. }
  25.  
  26. public static void main(String[] args) throws IOException, InterruptedException {
  27.     int collisions = 0, totazzl = 0;
  28.     int w = 2000, d = 2000;
  29.     HashMap<Integer, String> aaaamap = new HashMap<Integer, String>((w*d) * 5);
  30.     for(int x = -w; x < w; x++) {
  31.         for (int z = -d; z < d; z++) {
  32.             int hash = hash(x, z);
  33.             if (aaaamap.put(hash, "") != null) {
  34.                 collisions++;
  35.                 if (collisions < 20) {
  36.                     System.out.println(RZFormats.format("Collision: {0},{1} ({2})", x, z, hash(x, z)));
  37.                 }
  38.             }
  39.             totazzl++;
  40.         }
  41.     }
  42.     System.out.println(collisions + " / " + totazzl + " collisions");
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement