Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Random array sorting

by kidd (Curate)
on Sep 22, 2002 at 15:10 UTC ( [id://199901]=perlquestion: print w/replies, xml ) Need Help??

kidd has asked for the wisdom of the Perl Monks concerning the following question:

Hello...

I want to know if there is a way in which I can randomnize(?) the content in an array.

In example:
Lets say I have this array with names:

my @array = ('uriel', 'daniel', 'joel', 'samuel');

Now what I want is create a process so every time I print the array it prints the names in different order.


I've already tried this without success because it returns a number:

#!/usr/bin/perl -w use strict; my @array = ('uriel', 'daniel', 'joel', 'samuel'); print "Array before random: @array\n\n"; my @n_array = rand(@array); print "Array after random: @n_array";

I thoght it might work but it doesnt. I hope someone could give me an idea to work this out...

Thanks

Replies are listed 'Best First'.
•Re: Random array sorting
by merlyn (Sage) on Sep 22, 2002 at 15:22 UTC
    The answer's on your own hard drive, in the form of perldoc -q shuffle:
    Found in /opt/perl/snap/lib/5.8.0/pods/perlfaq4.pod How do I shuffle an array randomly? If you either have Perl 5.8.0 or later installed, or if yo +u have Scalar-List-Utils 1.03 or later installed, you can say: use List::Util 'shuffle'; @shuffled = shuffle(@list); If not, you can use a Fisher-Yates shuffle. sub fisher_yates_shuffle { my $deck = shift; # $deck is a reference to an ar +ray my $i = @$deck; while ($i--) { my $j = int rand ($i+1); @$deck[$i,$j] = @$deck[$j,$i]; } } # shuffle my mpeg collection # my @mpeg = <audio/*/*.mp3>; fisher_yates_shuffle( \@mpeg ); # randomize @mpeg i +n place print @mpeg; Note that the above implementation shuffles an array in pl +ace, unlike the List::Util::shuffle() which takes a list and re +turns a new shuffled list. You've probably seen shuffling algorithms that work using splice, randomly picking another element to swap the curre +nt element with srand; @new = (); @old = 1 .. 10; # just a demo while (@old) { push(@new, splice(@old, rand @old, 1)); } This is bad because splice is already O(N), and since you +do it N times, you just invented a quadratic algorithm; that is, O(N**2). This does not scale, although Perl is so efficien +t that you probably won't notice this until you have rather largi +sh arrays.

    -- Randal L. Schwartz, Perl hacker

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Random array sorting
by Aristotle (Chancellor) on Sep 22, 2002 at 15:15 UTC

    See merlyn's reply below.

    sub shuffle { my @shuffled; push @shuffed, splice(@_, rand(@_), 1) while @_; return @shuffled; } print shuffle @array;

    The splice pulls a random element out of the array, which is pushed onto the new array. Repeat while there are still elements in the list.

    Makeshifts last the longest.

Re: Random array sorting
by FloydATC (Deacon) on Apr 03, 2013 at 10:30 UTC
    Before discovering List::Util I used this:
    sub shuffled { my @ordered = @_; my @shuffled = (); while (@ordered) { my $i = int(rand() * @ordered); push @shuffled, $ordered[$i]; splice(@ordered, $i, 1); } return @shuffled; } print join(",", shuffled(1,2,3,4,5)) . "\n";

    ...but I don't anymore.

    -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://199901]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found