# celus

Members

3

1 Neutral

• Rank
Newbie
1. ## js-tutorial array sorting - sorting randomly

Hello Thanks for your answer. The script you posted shows an equal distribution, that's true. I tried changing it the following way to spot the difference of your script and mine: I moved the line var points = ... in the loop: var a, i, j var result = [0, 0, 0, 0, 0, 0, 0, 0, 0]; for(i = 0; i < 1000000; i++) { var points = [1, 0, 0, 0, 0, 0, 0, 0, 1]; a = points.sort(s); for(j = 0; j < a.length; j++) { result[j] += a[j]; } } document.getElementById("demo").innerHTML = result.join(", "); function s(a, {return 0.5 - Math.random()} It seems in the line a =
2. ## js-tutorial array sorting - sorting randomly

Hi everybody I'm not saying the Math.random() function isn't random enough. (I should have posted my text under suggestions.) I think the different browsers have different sorting algorithms in the background. Even if the random number generator is good, the sorting algorithm of most browsers doesn't treat all numbers in the array the same. As it seems to me, in most browsers the last element of the array is inserted/sorted last. Now if we define the sorting function e.g. as a < b, if the last element in the array is the smallest, it moves through the whole array. But if we def
3. ## js-tutorial array sorting - sorting randomly

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 your
×
×
• Create New...