Permalink
Browse files

Fixed documentation and added commments to pick() #145

  • Loading branch information...
qrazhan committed Dec 5, 2011
1 parent b9147e9 commit b7593162ace7cadf7283cec06e7cb07b4b4416cb
Showing with 6 additions and 5 deletions.
  1. +4 −4 lib/perl5i.pm
  2. +2 −1 lib/perl5i/2/ARRAY.pm
View
@@ -637,11 +637,11 @@ Example usage:
=head3 pick
- my @rand = @array->pick($someNumber);
+ my @rand = @array->pick($number);
+
+The pick() method returns a list of $number elements in @array.
+If $number is larger than the size of the list, it returns the entire list shuffled.
-The pick() method returns a list of $someNumber elements in @array.
-If $someNumber is larger than the size of the list, it returns the entire list shuffled.
-This algorithm works by generating a random number that gives each element an equal chance of being picked.
Example usage:
my @array = (1, 2, 3, 4);
View
@@ -49,7 +49,8 @@ method as_hash{
return wantarray ? %result : \%result;
}
-
+# for the first position in the array, generate a random number that gives that element an n/N chance of being picked (where n is the number of elements to pick and N is the total array size);
+# repeat for the rest of the array, each time altering the probability of the element being picked to reflect the number of elements picked so far and the number left.
method pick ( $num ){
Carp::croak("pick() takes the number of elements to pick")
unless defined $num;

0 comments on commit b759316

Please sign in to comment.