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..

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

.5 - Math.random() is not valid for determining random shuffling.
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.
Ah, looked up some info on it. Won't give very good distribution apparently. stackoverflow.com/questions/962802/… 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.
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.
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.

Comments

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) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.