Symmetric Bijective Algorithm for Integers

后端 未结 11 852
甜味超标
甜味超标 2020-12-01 03:33

I need an algorithm that can do a one-to-one mapping (ie. no collision) of a 32-bit signed integer onto another 32-bit signed integer.

My real concern is enough entr

11条回答
  •  既然无缘
    2020-12-01 04:19

    Can you use a random generated lookup-table? As long as the random numbers in the table are unique, you get a bijective mapping. It's not symmetric, though.

    One 16 GB lookup-table for all 32 bit values is probably not practical, but you could use two separate 16-bit lookup tables for the high-word and the low word.

    PS: I think you can generate a symmetric bijective lookup table, if that's important. The algorithm would start with an empty LUT:

    +----+        +----+
    |  1 |   ->   |    |
    +----+        +----+
    |  2 |   ->   |    |
    +----+        +----+
    |  3 |   ->   |    |
    +----+        +----+
    |  4 |   ->   |    |
    +----+        +----+
    

    Pick the first element, assign it a random mapping. To make the mapping symmetric, assign the inverse, too:

    +----+        +----+
    |  1 |   ->   |  3 |
    +----+        +----+
    |  2 |   ->   |    |
    +----+        +----+
    |  3 |   ->   |  1 |
    +----+        +----+
    |  4 |   ->   |    |
    +----+        +----+
    

    Pick the next number, again assign a random mapping, but pick a number that's not been assigned yet. (i.e. in this case, don't pick 1 or 3). Repeat until the LUT is complete. This should generate a random bijective symmetric mapping.

提交回复
热议问题