[erlang-questions] Speedy unsort:shuffle/1,2 ?
Toby Thain
toby@REDACTED
Thu May 27 02:47:03 CEST 2010
On 27-May-10, at 2:58 AM, Jesper Louis Andersen wrote:
> 2010/5/26 Björn-Egil Dahlberg <egil@REDACTED>:
>> I while back there were some talk about an unsort or shuffle
>> operation for
>> lists. I too was in need of this since it very convenient in
>> testing, so I
>> wrote one.
>
> Etorrent has one in its utils library as well:
>
> http://github.com/jlouis/etorrent/blob/master/src/etorrent_utils.erl#L101
>
> It works by creating an analogy to shuffling a deck of cards and I am
> somewhat sure it will uniformly shuffle
It won't. Use Fisher-Yates or an equivalent method.
http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
--Toby
> (though I have not proved that
> it is the case). The code is originally by Taylor Campbell if memory
> serves. I also think this is an excellent candidate for a standard
> library function. Getting it right is quite difficult. Getting it fast
> and right is even more difficult. I also note that the
> binary-generation trick ought to be usable on this one.
>
> In Etorrent it is rarely called and every place where it is used, the
> client would work even if you omit the call. Hence, it has not seen
> much testing.
>
>
> --
> J.
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
More information about the erlang-questions
mailing list