hash-prospector | Automated integer hash function discovery | Hashing library
kandi X-RAY | hash-prospector Summary
kandi X-RAY | hash-prospector Summary
Automated integer hash function discovery
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of hash-prospector
hash-prospector Key Features
hash-prospector Examples and Code Snippets
Community Discussions
Trending Discussions on hash-prospector
QUESTION
I'm looking for a mixing function that given an integer from an interval <0, n) returns a random-looking integer from the same interval. The interval size n will typically be a composite non power of 2 number. I need the function to be one to one. It can only use O(1) memory, O(1) time is strongly preferred. I'm not too concerned about randomness of the output, but visually it should look random enough (see next paragraph).
I want to use this function as a pixel shuffling step in a realtime-ish renderer to select the order in which pixels are rendered (The output will be displayed after a fixed time and if it's not done yet this gives me a noisy but fast partial preview). Interval size n will be the number of pixels in the render (n = 1920*1080 = 2073600 would be a typical value). The function must be one to one so that I can be sure that every pixel is rendered exactly once when finished.
I've looked at the reversible building blocks used by hash prospector, but these are mostly specific to power of 2 ranges.
The only other method I could think of is multiply by large prime, but it doesn't give particularly nice random looking outputs.
What are some other options here?
...ANSWER
Answered 2019-Mar-27 at 14:24Here is one solution based on the idea of primitive roots modulo a prime:
If a
is a primitive root mod p
then the function g(i) = a^i % p
is a permutation of the nonzero elements which are less than p
. This corresponds to the Lehmer prng. If n < p
, you can get a permutation of 0, ..., n-1
as follows: Given i
in that range, first add 1, then repeatedly multiply by a
, taking the result mod p
, until you get an element which is <= n
, at which point you return the result - 1.
To fill in the details, this paper contains a table which gives a series of primes (all of which are close to various powers of 2
) and corresponding primitive roots which are chosen so that they yield a generator with good statistical properties. Here is a part of that table, encoded as a Python dictionary in which the keys are the primes and the primitive roots are the values:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install hash-prospector
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page