Skip to content

Commit 168671e

Browse files
committed
correctly implemented meaning of array_rand
1 parent e223c51 commit 168671e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/arrays/array_rand.txt

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ mixed array_rand ( array $input [, int $num_req = 1 ] )
99
**Perl
1010

1111
>|Perl|
12-
use List::Util qw(shuffle);
13-
@rand = shuffle @array;
12+
# FOR ARRAYS
13+
# when $num_req == 1:
14+
my $result = int rand @array;
15+
16+
# otherwise:
17+
use Data::Random qw/ rand_set /;
18+
my @results = rand_set ( set => [0 .. $#array], size => $num_req );
19+
20+
# FOR HASHES
21+
# when $num_req == 1:
22+
my @keys = keys %hash;
23+
my $result = $keys [ int rand @keys ];
24+
25+
# otherwise:
26+
use Data::Random qw/ rand_set /;
27+
my @keys = keys %hash;
28+
my @results = @keys [ rand_set ( set => [0 .. $#array], size => $num_req ) ];
1429
||<

0 commit comments

Comments
 (0)