in reply to Re: selecting N random lines from a file in one pass
in thread selecting N random lines from a file in one pass
Too bad your implementation is flawed:# shuffle - I'll do it in pure perl.
In stead of getting 24 different permutations, each appearing about 1,000 times, your algorithm produces only 6 permutations, each appearing about 4,000 times.use strict; use warnings; my $size = 4; my @array = 1 .. $size; my %count; foreach (1 .. 24_000) { my @sample = @array; my $i = $size; while ($i--) { my $j = int rand($i); @sample[$i, $j] = @sample[$j, $i]; } $count {"@sample"} ++; } foreach my $key (sort keys %count) { printf "%s: %4d\n", $key, $count {$key} } __END__ 2 3 4 1: 3999 2 4 1 3: 4029 3 1 4 2: 3969 3 4 2 1: 4000 4 1 2 3: 4031 4 3 1 2: 3972
Better use a module next time!
|
|---|