In the quest to create adeterministic number generation function, our aim is to construct a function where each input number generates a unique corresponding number without any duplicates.
Modular Arithmetic Solution:
The ingenious solution lies in modular arithmetic, particularly the Affine cipher. It employs the transformation formula:
f(P) = (mP + s) mod n
where:
For the uint64 range, a non-even value for m is suggested to avoid divisibility by 2.
Example Implementation:
import ( "fmt" ) func main() { m := uint64(39293) s := uint64(75321908) transform := func(p uint64) uint64 { return p * m + s } testValues := []uint64{1, 2, 3, 4, 5} for _, v := range testValues { fmt.Printf("%v -> %v\n", v, transform(v)) } }
This function ensures that for all possible uint64 input values, the generated transformed values are unique.
Adapting for Signed Integers:
For signed integers (int64), the approach remains similar. We convert inputs and outputs between uint64 and int64 to maintain unique mappings:
func signedTransform(p int64) int64 { return int64(transform(uint64(p))) }
By utilizing this deterministic function, developers can generate unique and reproducible numbers from any given input integer, making it an invaluable tool for various applications.
The above is the detailed content of How to Generate Unique Deterministic Integers from Another Integer?. For more information, please follow other related articles on the PHP Chinese website!