UNPKG
fisher-yates
Version:
latest (1.0.4)
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
A compact module to randomly sort an Array
github.com/dcousens/fisher-yates
dcousens/fisher-yates
fisher-yates
/
index.js
18 lines
(12 loc)
•
286 B
JavaScript
View Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module
.
exports
=
function
shuffle
(
array, rng
) { rng = rng ||
Math
.
random
const
result = []
for
(
let
i =
0
; i < array.
length
; ++i) {
const
j =
Math
.
floor
(
rng
() * (i +
1
))
if
(j !== i) { result[i] = result[j] } result[j] = array[i] }
return
result }