3

I have this simple array:

var RedirUrl = new Array(4);
RedirUrl[0] = 'http://mafi.se/mf_redir/new_install_'+this_version+'.html';
RedirUrl[1] = 'http://ifurls.com/mf_redir/new_install_'+this_version+'.html';
RedirUrl[2] = 'http://ez.se/xml-update/mf_redir/new_install_'+this_version+'.html';
RedirUrl[3] = 'http://ilovre.net/mf_redir/new_install_'+this_version+'.html';
RedirUrl[4] = 'http://rihel.com/mf_redir/new_install_'+this_version+'.html';

and then

RedirUrl.sort(function() {return 0.5 - Math.random()}) 

The last bit is what is confusing me. I understand the "sort" and I understand the Math.random but the return 0.5 confuses me... what exactly is that?

(Needless to say I downloaded it off the net as it does what i want it to do... but i just dont understand it.)

Ryan
  • 9,821
  • 22
  • 66
  • 101

6 Answers6

7

It's sorting the list of URLs with a sorting method that randomly returns values that are greater than or less than 0 in about half the cases each.

Math.random() returns a number betweeen 0 and 1. Therefore 0.5 - Math.random() is a randomly decided value between -0.5 and 0.5. About half of those values are greater than zero and half of those are less than zero.

So about half of the time the comparison function will say that the first object is greater than the second and half of the time it will say the opposite.

It's a simple way to randomly shuffle the array. As has been pointed out in the comments, it's not a good way to shuffle an array (because the distribution is not even).

This question has a working implementation of the known-good Fisher-Yates shuffle.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • +1: It's surely not a good shuffling algorithm. It can return contradictory results, such as "a – Shamim Hafiz - MSFT Jun 01 '11 at 12:02
  • 1
    http://adrianquark.blogspot.com/2008/09/how-to-shuffle-array-correctly.html seems interesting – mplungjan Jun 01 '11 at 12:02
  • What would be a better shuffling algorithm in your opinion/s? – Ryan Jun 01 '11 at 12:15
  • 1
    @Ryan [see this better distrubition](http://jsfiddle.net/Kxsw2/16/). It uses [`_.sortBy`](http://documentcloud.github.com/underscore/docs/underscore.html#section-27) which is not perfect but "good enough" – Raynos Jun 01 '11 at 12:27
1

sort() takes a function for comparing values in an array as it's argument. Here the sort() method is told to give a random number between 0.0 and 1.0, and the 0.5 is there to make the random number go between -0.5 to 0.5, this randomly saying lesser or greater than.

Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
1

A comparator function should return < 0 to indicate that the first value is smaller, > 0 to indicate that it's larger, and 0 to indicate equality. Math.random() returns a number between 0 and 1. So, by doing the subtraction, you get random ordering!

stevevls
  • 10,675
  • 1
  • 45
  • 50
1

This would shuffle the array. If a function is used withing Array.sort() then it usually has two parameters which are compared Array.sort(function(a,b){}). A return value less than 0 indicates that a is before b, greater than 0 that a is after b and 0 that they have the same order. Using 0.5 - Math.random() means that you should get values greater or less than 0 at random.

detaylor
  • 7,112
  • 1
  • 27
  • 46
1

Seems the script will randomly shuffle this array by having the compare function randomly returns a value between -0.5 and +0.5

Also it generates an array of length 4 but then fills it with 5 items

I suggest this format instead

var RedirUrl = [
'http://mafi.se/mf_redir/new_install_'+this_version+'.html',
'http://ifurls.com/mf_redir/new_install_'+this_version+'.html',
'http://ez.se/xml-update/mf_redir/new_install_'+this_version+'.html',
'http://ilovre.net/mf_redir/new_install_'+this_version+'.html',
'http://rihel.com/mf_redir/new_install_'+this_version+'.html'
]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

Here sort will be performing a sort based on the comparison function that you pass to it.

In your case, it is:

function() {return 0.5 - Math.random()}

Assuming you are familiar with comparison based sorting, the function should return a negative value indicating that the left hand side value is less, zero if they are equal and positive if left hand side value is greater than the right hand side value, much like C function strcmp().

Looking into the function that you have, it will try to perform a random sort, that is try to shuffle the array.

On an additional note, this kind of shuffling algorithm is not an ideal one. It will be self contradictory. Since everything will occur at random, it may lead to situations where say, a.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176