-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisherYatesDurstenfeldKnuthShuffle-1.2.min.js
8 lines (8 loc) · 1.71 KB
/
fisherYatesDurstenfeldKnuthShuffle-1.2.min.js
1
2
3
4
5
6
7
8
/*/////////////////////////////////////////////
fisherYatesDurstenfeldKnuthShuffle 1.2
https://github.com/macmcmeans/fisherYatesDurstenfeldKnuthShuffle/blob/master/fisherYatesDurstenfeldKnuthShuffle-1.2.min.js
///////////////////////////////////////////////
Copyright (c) 2018, 2020, 2021 W. "Mac" McMeans
LICENSE: BSD 3-Clause License
*/
function fisherYatesDurstenfeldKnuthShuffle(e,t,n){"use strict";let r=0,o=1,f=e.length,l=new Array(f);if(2===arguments.length&&"function"!=typeof t&&"number"!=typeof t&&"boolean"!=typeof t)throw new Error("𝗦𝗲𝗰𝗼𝗻𝗱 𝗮𝗿𝗴𝘂𝗺𝗲𝗻𝘁 𝗶𝗹𝗹𝗲𝗴𝗮𝗹 𝘁𝘆𝗽𝗲");for(1===arguments.length&&(t=!1),2!==arguments.length||"boolean"!=typeof t&&"number"!=typeof t||(n=void 0),2===arguments.length&&"function"==typeof t&&(n=t,t=!1),1===(t=Number(t))&&--o,n=void 0===n?function(){let e=new Uint32Array(2),t=new Uint8ClampedArray(1),n=0;return crypto.getRandomValues(e),crypto.getRandomValues(t),+(["0.","0.0"][n=t[0]>128?1:0]+e[0]+""+e[1])}:n,l=e.slice();--f;)l[r=Math.floor(n()*(f+o))]=[l[f],l[f]=l[r]][0];return l}function fisherYatesDurstenfeldKnuthUnshuffle(e,t,n){"use strict";let r=0,o=1,f=new Array(e.length),l=new Array(e.length),u=e.length;if(1===arguments.length)throw new Error("𝗡𝗼 𝙪𝙣𝙨𝙝𝙪𝙛𝙛𝙡𝙚 𝗸𝗲𝘆 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗲𝗱");if(2===arguments.length&&("boolean"==typeof t||"number"==typeof t))throw new Error("𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝙪𝙣𝙨𝙝𝙪𝙛𝙛𝙡𝙚 𝗸𝗲𝘆");2===arguments.length&&"function"==typeof t&&(n=t,t=!1),1===(t=Number(t))&&--o;for(let e=0;e<u;e++)l[e]=e;for(let e=u-1;e>0;e--)r=Math.floor(n()*(e+o)),l[e]=[l[r],l[r]=l[e]][0];for(let t=0;t<u;t++)f[l[t]]=e[t];return f}