Hi everybody
http://www.w3schools.com/js/js_array_sort.asp
If I'm right, the JS-Tutorial about sorting arrays randomly has a lack. The order of the array is kind of in a random order at the first glance, but the elements often stay at the same place as they were. This is true in the most for the element at the end of the array and the effect happens in most browsers, but not in all of them. I've written a script to test this. In these browsers the elements are not evenly distributed as they should: Chrome, IE, Edge, Opera. In Safari they were evenly distributed when I tried it. Try it yourself.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var sum;
var points;
var i, j;
sum = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for (i=0;i<1000;i++){
points = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];
points.sort(function(a, {return 0.5 - Math.random()});
for (j=0;j<points.length;j++){
sum[j]+=points[j];
}
}
document.getElementById("demo").innerHTML = sum;
</script>
</body>
</html>
What the code does: It creates an array with a leading 1 and a 1 at the last place and zeroes between (points). Then it sorts this array randomly and adds the resulting array to a sum array. Then it starts again with the specific array. The generating, sorting and adding is done a 1000 times. When you reload the page the summed up numbers are displayed. Result: In many browsers the last number is higher than the others, which shouldn't happen with correct random sorting.
Cheers, celus