3 # Print input data in random order.
5 # TODO FYS algorithm works on in-memory array; way to shuffle while
6 # streaming over the input, similar to "print random line from file?"
7 # (shuf, apparently, if you have that toolset)
11 die "error: no data supplied to shuffle\n" unless @input;
13 fisher_yates_shuffle
( \
@input );
17 # fisher_yates_shuffle( \@array ) : generate a random permutation
19 sub fisher_yates_shuffle
{
22 for ( $i = @
$array; --$i; ) {
23 my $j = int rand( $i + 1 );
25 @
$array[ $i, $j ] = @
$array[ $j, $i ];
30 # Report problems when writing to stdout (perldoc perlopentut)
31 unless ( close(STDOUT
) ) {
32 warn "error: problem closing STDOUT: $!\n";