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
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.
None.
let shuffledArray = fisherYatesDurstenfeldKnuthShuffle( sourceArray, [sattoloCycle], [externalRNG] );
let restoredArray = fisherYatesDurstenfeldKnuthUnshuffle( shuffledArray, [sattoloCycle], externalRNG );
// #๐ญ ๐ฎ๐ฝ๐ฝ๐น๐ ๐ฎ ๐ฏ๐ฎ๐๐ถ๐ฐ ๐๐ต๐๐ณ๐ณ๐น๐ฒ, ๐๐ต๐ฒ๐ฟ๐ฒ ๐๐ต๐ฒ ๐ด๐ฒ๐ป๐ฒ๐ฟ๐ฎ๐๐ผ๐ฟ ๐ถ๐ ๐ถ๐ป๐๐ฒ๐ฟ๐ป๐ฎ๐น๐น๐ ๐ถ๐ป๐ถ๐๐ถ๐ฎ๐น๐ถ๐๐ฒ๐ฑ ๐๐ถ๐๐ต ๐๐ฟ๐๐ฝ๐๐ผ.๐ด๐ฒ๐๐ฅ๐ฎ๐ป๐ฑ๐ผ๐บ๐ฉ๐ฎ๐น๐๐ฒ๐
> 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.
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
https://bost.ocks.org/mike/shuffle/compare.html
Google Chrome on Win 10 (x64)
-
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.
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:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
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.
-
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.