Permalink
Browse files

correctly implemented meaning of array_rand

  • Loading branch information...
1 parent e223c51 commit 168671e58c3e01ed8aec5ef3a03b403f6e745f87 @spooon committed Apr 15, 2011
Showing with 17 additions and 2 deletions.
  1. +17 −2 docs/arrays/array_rand.txt
View
@@ -9,6 +9,21 @@ mixed array_rand ( array $input [, int $num_req = 1 ] )
**Perl
>|Perl|
-use List::Util qw(shuffle);
-@rand = shuffle @array;
+# FOR ARRAYS
+# when $num_req == 1:
+my $result = int rand @array;
+
+# otherwise:
+use Data::Random qw/ rand_set /;
+my @results = rand_set ( set => [0 .. $#array], size => $num_req );
+
+# FOR HASHES
+# when $num_req == 1:
+my @keys = keys %hash;
+my $result = $keys [ int rand @keys ];
+
+# otherwise:
+use Data::Random qw/ rand_set /;
+my @keys = keys %hash;
+my @results = @keys [ rand_set ( set => [0 .. $#array], size => $num_req ) ];
||<

0 comments on commit 168671e

Please sign in to comment.