Andrew Kensler's permute(), a function for stateless, constant-time pseudorandom-order array iteration
Web page · https://andrew-helmer.github.io/permute
First seen by Oriel on .
AI summary
The article explains Andrew Kensler’s permute(), which maps indices into a pseudorandom permutation with average constant-time queries and constant memory, enabling sampling without replacement.
- Unlike Fisher-Yates at O(n) time and memory, permute() processes k unique items in O(k) time and O(1) memory.
- An invertible seeded hash creates a bijection over a power-of-two domain, while cycle-walking repeatedly hashes out-of-range results until they fall below the array length.
- A bit mask limits the hash domain to less than twice the array length, producing fewer than two cycle-walking iterations on average.
- Independent constant-time index queries allow parallel use across threads or computers without storing permutation state.
- The function is not cryptographically secure, its hash works well only up to domains around 2^27, and a 64-bit cast prevents overflow-related repeats.
#pseudorandom permutations #random sampling #hash functions #c++
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Points
Score reported by the source.
- Lobsters: 25, latest observation ; +21 over observed period since
Comments
Comment count reported by the source.
- Lobsters: 3, latest observation ; +3 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Lobsters | pdzud6 | https://andrew-helmer.github.io/permute/ Read discussion |