4 ms·
summary of discussion: the top 5 are sorted by passing a comparison function to Array.sort() that returns a positive or negative number at random. at first gla
by andrewcooke 17y ago
summary of discussion: the top 5 are sorted by passing a comparison function to Array.sort() that returns a positive or negative number at random. at first glance this seems reasonable, but in fact whether it actually results in a random order depends on how the javascript sort function is implemented (and the initial order).
- gjm11 17y agoIt's "obvious" (in a somewhat unreasonable sense of that word) that this can't give a truly random permutation of the elements of the array. Since the only randomness comes from coin-flips, the probability of any particular outcome must be a rational number whose denominator is a power of 2. But there are n! permutations of n objects, and for n > 2 this isn't a power of 2, so they can't all be being selected with equal probability.
- andrewcooke 17y agoi've been thinking about this.... take three coins, arrange them from left to right. i will show how to select one of the three coins with probability 1/3. toss all three. repeat until only one shows heads. select that coin. so i think you are wrong :o)