# [1,2,3,4,5,6,7,8,9].shuffle()

• # July 20, 2013 at 1:27 pm

Make this code work:

``````[1,2,3,4,5,6,7,8,9].shuffle()
// [2, 4, 1, 8, 9, 6, 5, 3, 7]
``````

Can somebody help? It seems I have to use prototype an maybe Math.randam(). Thanks

# July 20, 2013 at 1:42 pm

This will be a start:

``````Array.prototype.shuffle = function() {
// 'this' is the array
// return the shuffled array
};
``````
# July 20, 2013 at 4:10 pm

It’s from Object-oriented Javascript by Stoyan Stefanov, great book.

# July 20, 2013 at 4:33 pm

before sleep was that

``````Array.prototype.shuffle = function() {
var len = this.length;
for ( var i = 0; i < len; i++ ) {
var ele = this*, pos = this[Math.floor(Math.random()*len)];
if(pos == undefined) {
pos = ele;
}
}
return this;
};
``````
# July 21, 2013 at 4:00 am

Give me, please some tips, here my result (don’t want to look at Stock overflow, I know there is answer, want to properly uderstand):

Array.prototype.shuffle = function() {
var len = this.length,
shuffleArr = [];

for ( var i = 0; i < len; i++ ) {
var elem = this,
pos = Math.floor( Math.random() * len );

if ( shuffleArr[pos] == undefined) {
shuffleArr[pos] = elem
shuffleArr.push(elem);
}

}

return shuffleArr;
}

var a =[1,2,3,4,5,6,7,8,9];

a.shuffle()

# July 21, 2013 at 6:51 am

You’re close, but you’re writing all the elements of the original array to a random index of the shuffled array (and if that index is taken, you just skip one element, which is bad). I don’t get why you added `shuffleArr.push(elem)` though, probably because the resulting array shorter than expected (which is caused by skipping elements if the random index is already taken).

If you want to keep the original array unchanged (returning a new, shuffled array). You can do it like that, except use `splice` to insert the element to a random position in the shuffled array.

for (var i = 0; i < len; i++) {
var elem = this,
pos = Math.floor( Math.random() * (shuffleArr.length + 1) ); // the +1 is needed, try without and see the last element of the shuffled array is always 1

shuffleArr.splice(pos, 0, elem);
}

Alternatively, you can change the original array. In pseudo-code:

for each element in original
find a random index i
swap that element with the element on index i

# July 21, 2013 at 7:30 am

It’s nice solution **shuffleArr.splice(pos, 0, elem);** I didn’t know how to make that elem go to different position, because Math.random() can repeat themselves, thats why there was this strange

if ( shuffleArr[pos] == undefined)

but, why you use:

pos = Math.floor( Math.random() * (shuffleArr.length + 1) )

this works too:

pos = Math.floor( Math.random() * len );

# July 21, 2013 at 11:03 am

For example, if your `shuffleArr` has 3 elements [1,2,3] and you want to add a 4th element, possible positions are 0, 1, 2 or 3 (resulting in [4,1,2,3], [1,4,2,3], [1,2,4,3] or [1,2,3,4]). `(shuffleArr.length + 1)` distributes evenly (same chance) between 0 and 3 (3 included, because of the +1).

If you specify an index too large to the `splice` method, for example 8… it just puts the element at the end of the array ([1,2,3,4]), that’s why `len` instead works too. The result is not distributed evenly though, that’s why you should use it. (You can actually see this if you shuffle it 100 times, you’ll notice a pattern where higher numbers have a higher chance to end up later in the array)

# July 21, 2013 at 4:02 pm

You should NOT use `len` I mean. Can’t edit my post.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".