[Wellington-pm] Select at random from a list

michael at diaspora.gen.nz michael at diaspora.gen.nz
Fri Jul 14 21:49:34 PDT 2006

Philip Plane writes:
>$mp3 = $playlist[int(rand(scalar(@playlist)))];

One feature I found when playing with Python recently that I really
liked was a better method of choosing a random element from an array:

    >>> import random
    >>> random.choice([1,2,3,4])
    >>> random.sample([1,2,3,4], 2)
    [4, 2]
    >>> x = [1,2,3,4]
    >>> random.shuffle(x)
    >>> x
    [3, 1, 2, 4]

A quick look on CPAN doesn't seem to reveal such a convenient module.
Anyone seen one, or should I think about a port?
    -- michael.

More information about the Wellington-pm mailing list