Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: RE (tilly) 2: Randomize an array

by blazar (Canon)
on Aug 01, 2008 at 20:51 UTC ( #701772=note: print w/replies, xml ) Need Help??

in reply to RE (tilly) 2: Randomize an array
in thread Randomize an array

Having 0 in that list is a mistake since the qsort algorithm takes that to mean that they are equal and hence it only needs to use one of those to compare with others.

I personally believe that before someone else points out that the node I'm replying to is some eight years old, I'd better do so myself: the reason why I'm doing it that I've seen this very thread referenced quite recently; which means that some people may still be looking here for info they will try to actually use. And here a terribly wrong technique has been advertised, so I'm writing this post for the benefit of potential readers to the effect of warning them not to use it! Namely, even if amended of the "0 in that list" problem, the general idea of shuffling a list by sorting it with a routine returning random values e.g.:

my @shuffled = sort {.5<rand} @input;

is flawed! Several reasons why it is are explained in a thread which came two years later. To put it briefly, if you really want to use sort for this, at the very least you must generate the list of random values in advance, whatever actual technique you actually choose in the end. E.g.:

my %r; my @shuffled = sort { ($r{$a}||=rand) <=> ($r{$b}||=rand) } @input;


my @r = map rand, @input; my @shuffled = @input[sort {$r[$a] <=> $r[$b]} 0..$#input];

Update: as a novice, back in my old days at the Monastery whenever I happened to see a post of mine which -exactly like this one and its companion- turned out to have a negative reputation, I would edit it to the effect of inserting an update and whine about the downvotes or more precisely asking why it earned them. Of course, I've learnt how to do better in the meantime, and generally refrain to. Except this time: since I would like to "thank" all those geniuses who downvoted this node on part of the poor newbie who will stumble upon it one day, and judging from the reputation will think that my complaints are moot, thus possibly following the advice of the broken shuffling technique... only to be bitten in the neck, but hopefully not later than having spread the word, because "it is so cool..."

If you can't understand the incipit, then please check the IPB Campaign.

Replies are listed 'Best First'.
Re^2: RE (tilly) 2: Randomize an array
by tilly (Archbishop) on Aug 01, 2008 at 21:00 UTC
    First of all, I did not recommend that sorting method.

    Secondly in this very thread I already explained why this randomization technique could not possibly provide perfectly random answers.

    So while I commend your desire to add useful information to old threads, I don't think you picked the right target in this case.

      First of all, I did not recommend that sorting method.

      I personally believe that I did never claim nor imply that you did; indeed you may notice that in the post you're replying to, I wrote: "a terribly wrong technique has been advertised" and the link under "advertised" points to a post of BooK. I replied to yours because one may have the impression that removing the "0 inconvenience" the technique may work after all: admittedly, I had overlooked that other post of yours, but then perhaps others would as well. I hope that you take no offense and that you will agree that the caveat earned much more visibility with the aid of these interventions...

      If you can't understand the incipit, then please check the IPB Campaign.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://701772]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-02-24 21:17 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (363 votes). Check out past polls.