MCPcopy
hub / github.com/stemkoski/stemkoski.github.com / shuffle

Function shuffle

MathBox/mathbox-bundle.js:39000–39011  ·  view source on GitHub ↗

* Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. * * @static * @memberOf _ * @category Collections * @param {Array|Object|string} collection The collection to shuffle. * @re

(collection)

Source from the content-addressed store, hash-verified

38998 * // => [4, 1, 6, 3, 5, 2]
38999 */
39000 function shuffle(collection) {
39001 var index = -1,
39002 length = collection ? collection.length : 0,
39003 result = Array(typeof length == 'number' ? length : 0);
39004
39005 forEach(collection, function(value) {
39006 var rand = baseRandom(0, ++index);
39007 result[index] = result[rand];
39008 result[rand] = value;
39009 });
39010 return result;
39011 }
39012
39013 /**
39014 * Gets the size of the `collection` by returning `collection.length` for arrays

Callers 1

sampleFunction · 0.85

Calls 2

baseRandomFunction · 0.85
forEachFunction · 0.70

Tested by

no test coverage detected