my ($n, $file); opendir DIR, $path or die "can't opendir $path: $!"; while (defined (my $f = readdir DIR)) { next if -d "$path/$f"; $file = $f if rand(++$n) < 1; } closedir DIR; $file = "$path/$f";