Back to all stories

Andrew Kensler's permute() function is a stateless, constant-time pseudorandom-order array iteration technique that allows for the iteration of k items from an array of length n in a random order, without duplicates, and is particularly useful in graphics, especially offline rendering, as well as cryptography and other fields. The function solves the problem of generating a random sample of k items without replacement, and its techniques are applicable beyond graphics and cryptography, offering an efficient alternative to traditional methods like the Fisher-Yates shuffle, with the added benefits of being invertible, using cycle-walking to achieve average constant-time performance.