in reply to Re: Re: Selecting random records from an array
in thread Selecting random records from an array

You bring up a great point. Its a new day, and giving it another look I'd imagine that using rand() within a sort algorythm might be bad -- perhaps cause infinite looping because the comparsion between two items keeps changing. Apparently it doesn't though, but perhaps the default sort might with a larger set, or might change in the future. How's this?

I guess I should only do the rand once and memorize the result.

my @foo = qw( a b c d e f g h i j k l m n o p q r s t u v w y z ); my %comp; my @feh = sort { if (! defined $comp{$a} ) { if (rand(1) >= .5) { $comp{$a} = 1 } else { $comp{$a} = 0; } } $comp{$a}; } @foo; my $upper = int( scalar(@feh) * .75 ); print join (",", @feh[ 0 .. $upper]); #C:\>perl randsort.pl (output) #b,a,d,c,f,e,h,g,i,j,k,l,m,n,r,q,p,o,s