Skip to content

๐Ÿ”€ The standard algorithm for generating a uniformly chosen random permutation. Devised by Ronald Fisher and Frank Yates, modernized by Richard Durstenfeld and popularized by Donald E. Knuth. This version permits Sattolo cycles as well as seeded/keyed shuffles and unshuffles.

License

Notifications You must be signed in to change notification settings

macmcmeans/fisherYatesDurstenfeldKnuthShuffle

Repository files navigation

๐Ÿ”€ fisherYatesDurstenfeldKnuthShuffle (and unShuffle)
Maintenance License

The original pencil-and-paper logic created by Fisher and Yates in 1938 had a time complexity of O(n2). In 1964, Durstenfeld modified the algorithm to O(n). Knuth made the classic shuffle famous in his 1968 book, The Art of Computer Programming. And somewhere down the line, somebody somehow figured out some way to make the "inside-out" version, which is the logic I present here.

At minimum, it can be called by supplying a single-dimension array of values. If an external random number generator (RNG) is not referenced, the array will be shuffled non-deterministically using values from Crypto.getRandomValues(), which would be suitable for Monte Carlo applications. For cryptography purposes, an external cryptographically secure RNG (such as ISAAC) should be specified to produce deterministic, seeded shuffling and unshuffling.

The shuffle produces statistically flat output, and the lack of bias may be checked using this visual tool.

Optionally, Sattoloโ€™s algorithm may be used to generate random cyclic permutations of length n instead of random permutations. This is useful when the condition to be met involves no element of the array ever ending up in its original position (derangement) but is wholly unsuited for cryptography.

This version provides function overloading using internal logic having no external dependencies. Any optional parameters will be set to default values when omitted.


ย 
Version 1.2
Author: W. "Mac" McMeans
Date: 21 MAY 2021
ย 

Application:

Use this when you need an unbiased shuffle that runs in O(n) time. Couple it with a cryptographically secure pseudo random number generator, such as ISAAC, for cryptographic applications.

Dependencies:

None.
ย 

Syntax:

let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( sourceArray, [sattoloCycle], [externalRNG] );
ย 
let restoredArray = fisherYatesDurstenfeldKnuthUnshuffle( shuffledArray, [sattoloCycle], externalRNG );
ย 

Example usage:

// #๐Ÿญ ๐—ฎ๐—ฝ๐—ฝ๐—น๐˜† ๐—ฎ ๐—ฏ๐—ฎ๐˜€๐—ถ๐—ฐ ๐˜€๐—ต๐˜‚๐—ณ๐—ณ๐—น๐—ฒ, ๐˜„๐—ต๐—ฒ๐—ฟ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ด๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ ๐—ถ๐˜€ ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ๐—ป๐—ฎ๐—น๐—น๐˜† ๐—ถ๐—ป๐—ถ๐˜๐—ถ๐—ฎ๐—น๐—ถ๐˜‡๐—ฒ๐—ฑ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฟ๐˜†๐—ฝ๐˜๐—ผ.๐—ด๐—ฒ๐˜๐—ฅ๐—ฎ๐—ป๐—ฑ๐—ผ๐—บ๐—ฉ๐—ฎ๐—น๐˜‚๐—ฒ๐˜€
> let _theArray = [0,1,2,3];
> let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( _theArray );
> console.log( shuffledArray );                      --> (4) [0, 2, 1, 3] 


// #๐Ÿฎ ๐—ฎ๐—ฝ๐—ฝ๐—น๐˜† ๐—ฆ๐—ฎ๐˜๐˜๐—ผ๐—น๐—ผโ€™๐˜€ ๐—ฎ๐—น๐—ด๐—ผ๐—ฟ๐—ถ๐˜๐—ต๐—บ ๐˜๐—ผ ๐˜๐—ต๐—ฒ ๐˜€๐—ต๐˜‚๐—ณ๐—ณ๐—น๐—ฒ (๐—ป๐—ผ๐˜๐—ฒ ๐˜๐—ต๐—ฎ๐˜ ๐—ฒ๐—ฎ๐—ฐ๐—ต ๐—ฒ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ฒ๐—ป๐—ฑ๐˜€ ๐˜‚๐—ฝ ๐—ถ๐—ป ๐—ฎ ๐—ป๐—ฒ๐˜„ ๐—ฝ๐—ผ๐˜€๐—ถ๐˜๐—ถ๐—ผ๐—ป)
> let _theArray = [0,1,2,3];
> let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( _theArray, true );
> console.log( shuffledArray );                      --> (4) [3, 2, 0, 1] 


// #๐Ÿฏ ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ฎ๐—ป ๐—ฒ๐˜…๐˜๐—ฒ๐—ฟ๐—ป๐—ฎ๐—น ๐—ฅ๐—ก๐—š ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ (๐—ป๐—ผ๐˜๐—ฒ ๐˜๐—ต๐—ฒ ๐—บ๐—ฒ๐˜๐—ต๐—ผ๐—ฑ ๐—ผ๐˜ƒ๐—ฒ๐—ฟ๐—น๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐˜„๐—ต๐—ฒ๐—ฟ๐—ฒ ๐˜๐—ต๐—ฒ ๐Ÿฏ๐—ฟ๐—ฑ ๐—ฝ๐—ฎ๐—ฟ๐—บ ๐—ถ๐˜€ ๐—ฐ๐—ฎ๐—น๐—น๐—ฒ๐—ฑ ๐—ฎ๐˜€ ๐˜๐—ต๐—ฒ ๐Ÿฎ๐—ป๐—ฑ ๐—ฎ๐—ฟ๐—ด)
> const _simpleRNG = function() { return Math.random(); }
> let _theArray = [0,1,2,3];
> let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( _theArray, _simpleRNG );
> console.log( shuffledArray );                      --> (4) [0, 3, 1, 2] 


// #๐Ÿฐ ๐—ถ๐—ป๐˜ƒ๐—ผ๐—ธ๐—ฒ ๐—ฎ ๐—ฆ๐—ฎ๐˜๐˜๐—ผ๐—น๐—ผ ๐˜€๐—ต๐˜‚๐—ณ๐—ณ๐—น๐—ฒ ๐—ฎ๐—ป๐—ฑ ๐˜‚๐˜€๐—ฒ ๐—ฎ๐—ป ๐—ฒ๐˜…๐˜๐—ฒ๐—ฟ๐—ป๐—ฎ๐—น ๐—ฅ๐—ก๐—š
> const _simpleRNG = function() { return Math.random(); }
> let _theArray = [0,1,2,3,4,5];
> let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( _theArray, true, _simpleRNG );
> console.log( shuffledArray );                      --> (6) [4, 2, 3, 0, 5, 1] 


// #๐Ÿฑ ๐˜€๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ฒ๐—น๐˜† ๐˜€๐—ต๐˜‚๐—ณ๐—ณ๐—น๐—ฒ ๐—ฎ๐—ป๐—ฑ ๐—ฟ๐—ฒ๐˜€๐˜๐—ผ๐—ฟ๐—ฒ ๐—ฎ ๐˜€๐˜๐—ฟ๐—ถ๐—ป๐—ด ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—ฎ๐—ป ๐—ฒ๐˜…๐˜๐—ฒ๐—ฟ๐—ป๐—ฎ๐—น ๐—ฐ๐—ฟ๐˜†๐—ฝ๐˜๐—ผ๐—ด๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ถ๐—ฐ๐—ฎ๐—น๐—น๐˜† ๐˜€๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ฒ ๐—ฅ๐—ก๐—š (๐—ณ๐—ผ๐—ฟ ๐—ฒ๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ, ๐™ž๐™จ๐™–๐™–๐™˜๐˜พ๐™Ž๐™‹๐™๐™‰๐™‚)
> const _theString = 'this is my test string', _theArray = _theString.split( '' );
> const _secureRNG = isaacCSPRNG( 'this is my seed' );    // init RNG
> const shuffledArray = fisherYatesDurstenfeldKnuthShuffle( _theArray, _secureRNG.double );
> console.log( shuffledArray );                      --> (22)ย ["t", " ", "h", "y", "i", "t", "r", "n", "t", "s", "g", "i", " ", "i", "s", " ", "e", " ", "t", "m", "s", "s"]

> _secureRNG.seed( 'this is my seed' );    // reset RNG
> const restoredArray = fisherYatesDurstenfeldKnuthUnshuffle( shuffledArray, _secureRNG.double );
> console.log( restoredArray );                      --> (22)ย ["t", "h", "i", "s", " ", "i", "s", " ", "m", "y", " ", "t", "e", "s", "t", " ", "s", "t", "r", "i", "n", "g"]
> let _newString = restoredArray.join( '' );
> console.log( _newString );                         --> "this is my test string"

// NOTE: The use of constants in this example is to make clear that copies of arrays are returned (no copy in-place); the original arrays are not altered.


ย 

REFS:

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

https://bost.ocks.org/mike/shuffle/compare.html
ย 

Tested:

Google Chrome on Win 10 (x64)
ย 

Version notes:

  • 1.2 - 21 MAY 2021
    bug Fix error in unshuffle logic where the restore loop was too short.
    update Functions return array copies, source arrays are not modified.
    update Revise readme.
    ย 

  • 1.1 - 4 MAY 2020
    feature Include an unshuffle function.
    update Code cleanup.
    ย 

  • 1.0 - 26 MAY 2018
    release Initial release.
    ย 

License (BSD)

Copyright ยฉ 2018, 2020, 2021 W. "Mac" McMeans

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

๐Ÿ”€ The standard algorithm for generating a uniformly chosen random permutation. Devised by Ronald Fisher and Frank Yates, modernized by Richard Durstenfeld and popularized by Donald E. Knuth. This version permits Sattolo cycles as well as seeded/keyed shuffles and unshuffles.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published