Skip to content

Commit 8a3c0a0

Browse files
committed
Export Fisher Yates shuffling algorithm
1 parent 924c77d commit 8a3c0a0

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/shuffle/fisheryates.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
/**
2-
* The shuffling algorithm of
3-
* Fisher-Yates. Complexity O(n)
4-
*
5-
* @param {array} array The array which should be shuffled
6-
* @return {array} The shuffled array.
7-
*/
8-
function shuffle(array) {
1+
(function (exports) {
92
'use strict';
10-
var size = array.length,
11-
rand, temp;
12-
for (var i = 1; i < size; i += 1) {
13-
rand = Math.round(Math.random() * i);
14-
temp = array[rand];
15-
array[rand] = array[i];
16-
array[i] = temp;
3+
4+
/**
5+
* The shuffling algorithm of
6+
* Fisher-Yates. Complexity O(n)
7+
*
8+
* @param {array} array The array which should be shuffled
9+
* @return {array} The shuffled array.
10+
*/
11+
function shuffle(array) {
12+
var size = array.length,
13+
rand, temp;
14+
for (var i = 1; i < size; i += 1) {
15+
rand = Math.round(Math.random() * i);
16+
temp = array[rand];
17+
array[rand] = array[i];
18+
array[i] = temp;
19+
}
20+
return array;
1721
}
18-
return array;
19-
}
22+
23+
exports.shuffle = shuffle;
24+
25+
}(typeof exports === 'undefined' ? window : exports));
2026

0 commit comments

Comments
 (0)