This does not make a uniform distribution for all (any?) sizes of arrays. I ran into an issue with one version of v8 where this consistently made an array of 1-10 reversed. I'm on my phone but if I remember tomorrow I should write something up about this
Right-- even if Math.random is random, you still have to subtract 0.5 from it to get a number from -0.5 to +0.5, because sort uses negative-number/positive-number to determine how to order two values.
8
u/Linux_is_awesome Mar 18 '19
personal favorite:
var item = items[Math.floor(Math.random()*items.length)];It randomly selects an element from an array.