DEV Community

Discussion on: Why I don't like JavaScript

 
avalander profile image
Avalander • Edited

[].sort(() => Math.random() - 0.5) tends to not displace elements in the array a lot. An element might be moved a few positions forward or backward, but it will generally stay close to its original position. Therefore, when used to shuffle an array, the result will be more similar to the original array than with other algorithms.

As an example, here's the output of three runs with randSeq and three runs with randSeq2:

# randSeq
[ 1, 3, 4, 5, 9, 7, 6, 8, 2, 10 ]
[ 7, 1, 2, 3, 6, 8, 4, 5, 9, 10 ]
[ 2, 3, 1, 4, 6, 9, 8, 5, 7, 10 ]

# randSeq2
[ 5, 1, 7, 10, 4, 6, 3, 9, 8, 2 ]
[ 7, 1, 6, 9, 5, 8, 3, 4, 10, 2 ]
[ 2, 6, 4, 10, 9, 8, 5, 3, 1, 7 ]

You can notice that with the randSeq the array is less shuffled than with randSeq2 and the elements show a clear tendency to be larger the further back they are in the array.

Thread Thread
 
stephenramthun profile image
Stephen Ramthun

Makes sense. Thanks!