Objective
Generate a deterministic function that maps input numbers to unique output numbers within a specified range, without duplication.
Method
Utilize modular arithmetic based on the Affine cipher:
Parameters:
Example for uint64
Transformation Function:
func transform(p uint64) uint64 { return m * p + s // implicitly mod'd 2^64 by the type's size }
Example with int64
For signed numbers, convert input and output between uint64 and int64 to maintain the unique mapping:
func signedTransform(p int64) int64 { return int64(transform(uint64(p))) }
Guarantees
The above is the detailed content of How to Generate Unique, Deterministic Numbers with a 1:1 Mapping?. For more information, please follow other related articles on the PHP Chinese website!