Created
October 21, 2014 17:32
-
-
Save sergeevabc/e9868f6b38d3f834b6f8 to your computer and use it in GitHub Desktop.
Salted variation of Fisher-Yates shuffle http://hashids.org/javascript/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saltedShuffle(alphabet, salt) { | |
var integer, j, temp, i, v, p; | |
for (i = alphabet.length - 1, v = 0, p = 0; i > 0; i--, v++) { | |
v %= salt.length; | |
p += integer = salt[v].charCodeAt(0); | |
j = (integer + v + p) % i; | |
temp = alphabet[j]; | |
alphabet = alphabet.substr(0, j) + alphabet[i] + alphabet.substr(j + 1); | |
alphabet = alphabet.substr(0, i) + temp + alphabet.substr(i + 1); | |
} | |
return alphabet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment