1

What is the best way to randomize part of the array in Javascript

For example, if I have 100 items in the array, what is the fast and efficient way of randomizing set of every 10 times. Items between 0 and 9 is randomize within data items[0] to items[9]. Items between 10 to 19 are randomize within data items[10] to items[19] and so on..

Mark K
  • 1,224
  • 3
  • 20
  • 30

4 Answers4

3

You can adjust the array shuffle method that is described here: http://jsfromhell.com/array/shuffle

It is based on Fisher-Yates (Knuth) algorithm (http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).

Karel Petranek
  • 15,005
  • 4
  • 44
  • 68
1

I would just use the built in slice, concat and sort methods. Something like this.

function shuffle(arr, start, end) {
 return arr.slice(start, end).sort(function() { return .5 - Math.random(); }).concat(arr.slice(end));
};

That's the basic idea, you probably want to make an algorithm to slice every 10 elements, sort and rejoin the sets.

EDIT: Read comments below for why this solution may not be suitable for your needs.

Kyle Jones
  • 536
  • 3
  • 5
  • `.5 - Math.random()` is not valid for determining random shuffling. – lincolnk Oct 26 '10 at 21:05
  • you don't get "random" order you get "undetermined"(?) order. to get a valid random shuffle, running two items through the compare function has to produce a repeatable result. if you want an example, look up the situation with where MS had to provide a list of browser for the user to choose from when installing windows (win7 i think) The options were supposed to be presented in a random order but they screwed it up. – lincolnk Oct 26 '10 at 21:16
  • Ah, looked up some info on it. Won't give very good distribution apparently. http://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling Also, this method, while quick to code, is less efficient than the Fisher Yates shuffle suggested by dark_charlie, so that could be a concern for large arrays. – Kyle Jones Oct 26 '10 at 21:18
  • sort-on-random is likely to offer poor randomness as a shuffle method, depending on what sort algorithm the browser chooses. On Firefox and Opera the results of a sort-on-random are very clearly biased towards results that are only a little different from the starting position. IE seems to bias towards the reverse order. For example in a random-sort shuffle of the alphabet, IE puts A in the last place nearly half the time. – bobince Oct 26 '10 at 21:26
0

I would split the 1 array into 10 subsets, then perform a shuffle algorithm of your choice on those subsets, and then recombine in the correct order.

Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
0

The following shuffles the specified chunk of an array in place, as randomly as the environment's random number generator will allow:

function shuffleSubarray(arr, start, length) {
    var i = length, temp, index;
    while (i--) {
        index = start + Math.floor(i * Math.random());
        temp = arr[index];
        arr[index] = arr[start + i];
        arr[start + i] = temp;
    }
    return arr;
}

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
alert( shuffleSubarray(a, 2, 5) );
Tim Down
  • 318,141
  • 75
  • 454
  • 536