[].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:
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.
Nice catch, thanks! Didn't realise that the list had to be unique. Could you explain what you mean by
.sort()
not producing a lot of variations?[].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 withrandSeq2
:You can notice that with the
randSeq
the array is less shuffled than withrandSeq2
and the elements show a clear tendency to be larger the further back they are in the array.Makes sense. Thanks!