-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfisherYatesDurstenfeldKnuthShuffle-1.2.js
208 lines (151 loc) · 7.97 KB
/
fisherYatesDurstenfeldKnuthShuffle-1.2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*///////////////////////////////////////////////////////////////////////////////////////////////////
fisherYatesDurstenfeldKnuthShuffle 1.2
https://github.com/macmcmeans/fisherYatesDurstenfeldKnuthShuffle/blob/master/fisherYatesDurstenfeldKnuthShuffle.js
/////////////////////////////////////////////////////////////////////////////////////////////////////
This version is
Copyright (c) 2018, 2020, 2021 W. "Mac" McMeans
All rights reserved.
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.
/////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////*/
/**
* The Durstenfeld shuffle, a standard algorithm for generating a uniformly chosen random permutation.
* @param {array} _array - The array to be shuffled.
* @param {boolean} [_sattoloCycle=false] - An optional flag indicating whether a Sattolo Cycle shuffle should be produced.
* @param {function} [_rng] - An optional external key/seed useful to shuffle the array (required when deterministically shuffling).
* @returns {array} - A copy of _array in a shuffled state.
*/
const fisherYatesDurstenfeldKnuthShuffle = function( _array, _sattoloCycle, _rng ) {
'use strict';
////////////////////
// initialization //
////////////////////
let pickIndex = 0
, positionModifier = 1
, arrayPosition = _array.length
, tempArray = new Array( arrayPosition )
;
const csprng =
function() {
let uinta = new Uint32Array( 2 )
, mrand = new Uint8ClampedArray( 1 )
, prefix = [ '0.', '0.0' ]
, rindex = 0
;
crypto.getRandomValues( uinta );
crypto.getRandomValues( mrand );
rindex = mrand[ 0 ] > 128 ? 1 : 0
return +( prefix[ rindex ] + uinta[ 0 ] + '' + uinta[ 1 ] );
}
;
////////////////////
////////////////////
// error checking //
////////////////////
if(
2 === arguments.length
&&
(
'function' !== typeof _sattoloCycle
&&
'number' !== typeof _sattoloCycle
&&
'boolean' !== typeof _sattoloCycle
)
){
throw new Error( '𝗦𝗲𝗰𝗼𝗻𝗱 𝗮𝗿𝗴𝘂𝗺𝗲𝗻𝘁 𝗶𝗹𝗹𝗲𝗴𝗮𝗹 𝘁𝘆𝗽𝗲' );
}
////////////////////
/////////////////////////////////////////////
// logic that permits 'method overloading' //
/////////////////////////////////////////////
if( 1 === arguments.length ) {
_sattoloCycle = false;
}
if( 2 === arguments.length && ( 'boolean' === typeof _sattoloCycle || 'number' === typeof _sattoloCycle ) ) {
_rng = undefined;
}
if( 2 === arguments.length && 'function' === typeof _sattoloCycle ) {
_rng = _sattoloCycle;
_sattoloCycle = false;
}
/////////////////////////////////////////////
////////////////////////////////
// set defaults, as necessary //
////////////////////////////////
_sattoloCycle = Number( _sattoloCycle );
if( 1 === _sattoloCycle ) { --positionModifier; }
_rng = ( undefined === _rng ? csprng : _rng );
////////////////////////////////
/////////////////////////////////
// finally, the actual shuffle //
/////////////////////////////////
tempArray = _array.slice();
while( --arrayPosition ) {
pickIndex = Math.floor( _rng() * ( arrayPosition + positionModifier ) );
tempArray[ pickIndex ] = [ tempArray[ arrayPosition ], tempArray[ arrayPosition ] = tempArray[ pickIndex ] ][ 0 ];
}
/////////////////////////////////
return tempArray;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Complementary unshuffle logic for the above Durstenfeld shuffle.
* @param {array} _shuffledArray - The shuffled array to be restored.
* @param {boolean} [_sattoloCycle=false] - An optional flag indicating whether a Sattolo Cycle shuffle was produced when shuffled.
* @param {function} _rng - An external key/seed required to unshuffle the array.
* @returns {array} - A copy of the _shuffledArray in a reordered state.
*/
const fisherYatesDurstenfeldKnuthUnshuffle = function( _shuffledArray, _sattoloCycle, _rng ) {
'use strict';
////////////////////
// initialization //
////////////////////
let pickIndex = 0
, positionModifier = 1
, restoredArray = new Array( _shuffledArray.length )
, tempArray = new Array( _shuffledArray.length )
, shuffledArrayLength = _shuffledArray.length
;
////////////////////
/////////////////////////////////////////////
// logic that permits 'method overloading' //
/////////////////////////////////////////////
if( 1 === arguments.length ) {
throw new Error( '𝗡𝗼 𝙪𝙣𝙨𝙝𝙪𝙛𝙛𝙡𝙚 𝗸𝗲𝘆 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗲𝗱' );
}
if( 2 === arguments.length && ( 'boolean' === typeof _sattoloCycle || 'number' === typeof _sattoloCycle ) ) {
throw new Error( '𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝙪𝙣𝙨𝙝𝙪𝙛𝙛𝙡𝙚 𝗸𝗲𝘆' );
}
if( 2 === arguments.length && 'function' === typeof _sattoloCycle ) {
_rng = _sattoloCycle;
_sattoloCycle = false;
}
/////////////////////////////////////////////
////////////////////////////////
// set defaults, as necessary //
////////////////////////////////
_sattoloCycle = Number( _sattoloCycle );
if( 1 === _sattoloCycle ) { --positionModifier; }
////////////////////////////////
/////////////////////
// unshuffle logic //
/////////////////////
for( let arrayIndex = 0; arrayIndex < shuffledArrayLength; arrayIndex++ ) {
tempArray[ arrayIndex ] = arrayIndex;
}
for( let arrayIndex = shuffledArrayLength - 1; arrayIndex > 0; arrayIndex-- ) {
pickIndex = Math.floor( _rng() * ( arrayIndex + positionModifier ) );
tempArray[ arrayIndex ] = [ tempArray[ pickIndex ], tempArray[ pickIndex ] = tempArray[ arrayIndex ] ][ 0 ];
}
for( let arrayIndex = 0; arrayIndex < shuffledArrayLength; arrayIndex++ ) {
restoredArray[ tempArray[ arrayIndex ] ] = _shuffledArray[ arrayIndex ];
}
/////////////////////
return restoredArray;
}