diff --git a/src/array.js b/src/array.js new file mode 100644 index 00000000..1f4b6b96 --- /dev/null +++ b/src/array.js @@ -0,0 +1,17 @@ +export var slice = Array.prototype.slice; + +export function shuffle(array) { + var m = array.length, + i0 = 0, + t, + i; + + while (m) { + i = Math.random() * m-- | 0; + t = array[m + i0]; + array[m + i0] = array[i + i0]; + array[i + i0] = t; + } + + return array; +} diff --git a/src/pack/enclose.js b/src/pack/enclose.js index 0d89f409..96d471a7 100644 --- a/src/pack/enclose.js +++ b/src/pack/enclose.js @@ -1,5 +1,7 @@ +import {shuffle, slice} from "../array"; + export default function(circles) { - var i = 0, n = circles.length, B = [], p, e; + var i = 0, n = (circles = shuffle(slice.call(circles))).length, B = [], p, e; while (i < n) { p = circles[i];