Category: | Fun Stuff |
Author/Contact Info | agentzen@nmt.edu |
Description: | I think too much emphasis is placed on sorting lists. Bubblesort this, and quicksort that, and selection sort something else. But what about random shuffling? I never see any attention given to the opposite of sorting; shuffling! I wrote out some basic code that i think does the trick, but if anyone has any tips on how it can be improved, please let me know. thanks |
sub shuffle {
srand(time ^ ($$ + ($$ << 15))); # For pre-Perl 5.004.
$r_deck = shift;
$deck_l = scalar(@$r_deck);
for ($i=0; $i<$deck_l; $i++) {
$rand = int(rand($deck_l)); # Find random integer betwen
# 0 and one less than the length
# of the array (deck).
($r_deck->[$i], $r_deck->[$rand]) = ($r_deck->[$rand], $r_deck
+->[$i]);
# You gotta love Perl, don't you? #
# Switch their values. #
}
}
|
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Array Shuffler
by ChOas (Curate) on Jan 09, 2001 at 16:24 UTC | |
by runrig (Abbot) on Jan 09, 2001 at 21:28 UTC | |
by Adam (Vicar) on Jan 12, 2001 at 23:48 UTC | |
Re: Array Shuffler
by OeufMayo (Curate) on Jan 09, 2001 at 16:27 UTC | |
Re (tilly) 1: Array Shuffler
by tilly (Archbishop) on Jan 09, 2001 at 17:43 UTC | |
Re: Array Shuffler
by davorg (Chancellor) on Jan 09, 2001 at 16:22 UTC | |
by Dominus (Parson) on Jan 09, 2001 at 19:01 UTC |