I'm trying to understand how sorting an array in random order works. So, I found the following code:
var as = ["max","jack","sam"];
var s = as.sort(func);
function func(a, b) {
return 0.5 - Math.random();
}
console.log(s);
my main question is why they use 0.5 not another number? and how it really works please try to make it simple i'm new in javascript and i'm struggling with these things
Math.random() return random value between 0 to 1 (0 is included but 1 is excluded). So 0.5 act as mid point. If use use value like greater than 1 or less 0 than it will always be either true or false. So for this reason 0.5 is used.
You can read more here about Math.random()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
This is what you get when you use value greater than 1
This is what happens when you use value less than 0
P.S :-
Try printing output from all the above condition you will see that last two condition will always return either true or false from function. so you will not get a random sorting.
Now talk about any value from 0 to 0.99 you can use any value but 0.5 will serve your purpose best.Because it's a middle point you're most likely to get best answer.
Math.random
returns a number between 0 and 1.Sorting function use the return value
x
as the following :x == 0
: Same value, can order "how it wants"x < 0
: the first object is less than the second one, therefore its index in the sorted array will be less than the other'sx > 0
same asx < 0
but the other way aroundSince
Math.random
returns a number between 0 and 1 and we want to also get negative numbers, we must subtract some value. Here0.5 - Math.random()
would give a number between 0.5 and -0.5If you just want to nudge the elements near their starting positions in a random way, sure, use
sort
withrandom
, but in most cases, that's not what you want. You want to thoroughly shuffle an array, completely randomize the position of every element. And for thatrandom
in the built-insort
function is a terrible practice, because it is biased towards the initial state, meaning that the elements in the "shuffled" array will tend to stay near their positions (those that were near the beginning have the high probability of staying near the beginning, etc...). The bigger the size the array grows, the less it gets shuffled.Here is the proof: Is it correct to use JavaScript Array.sort() method for shuffling?
And here is the function for shuffling arrays I use most of the time. It thoroughly randomizes the position of every element.
Math.random() returns a number between
0
and1
(exclusive). We're using0.5
because it is the mean value.Array.sort() sorts the parameters based on the return value. So,
0.5 - Math.random()
will yield either positive or negative value with equal probability. Hence, it will sort the parameters randomly.How it really works
Array.sort()
is positive, then the index of the first parameter will be higher than that of the second.0
, then do nothing.You used
And here the most important thing is
as.sort(func)
.func(a,b)
will return value in range of[-0.5,0.5]
.Because this function return
0.5 - Math.random()
and Math.random() will return the float value which is in range of[0,1]
.So that your
func
will return value in range of[-0.5,0.5]
.And this mean that sort order will be set
increase
ordecrease
. this is random. So your result will be randomIf you call the
sort
method with a function parameter is called several times. This function should accept two parameters (let's call the first A and the second B) Each time it should return a value:So in this example we need random return values that evenly distribute negative and positive values. Since
Math.random()
returns a value between 0 and 1,0.5 - Math.random()
will return values between -0.5 and 0.5, which meets the requirements.