Permalink
Browse files

tweak _.shuffle

  • Loading branch information...
1 parent 7615764 commit adc778ae37a2c21c7baabf5f7babe43e19be09dd @clottes committed Feb 8, 2013
Showing with 4 additions and 6 deletions.
  1. +4 −6 underscore.js
View
@@ -740,12 +740,10 @@ var till = _.till = _.until = function(obj, iterator, context, pass) {
// Shuffle an array.
_.shuffle = function(obj) {
- var rand;
- var index = 0;
- var shuffled = [];
- each(obj, function(value) {
- rand = _.random(index++);
- shuffled[index - 1] = shuffled[rand];
+ var shuffled = [], rand;
+ each(obj, function(value,index) {
+ rand = Math.floor(Math.random() * (index + 1));
+ shuffled[index] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;

0 comments on commit adc778a

Please sign in to comment.