in reply to Re: Opening random files then reading random lines from file.
in thread Opening random files then reading random lines from file.

If I correctly understand the
rand($.) < 1 && ($line = $_) while <>
there is no need to know the number of the lines in the file.

Replies are listed 'Best First'.
Re^3: Opening random files then reading random lines from file.
by brx (Pilgrim) on Apr 27, 2012 at 09:18 UTC

    Yep. This idea is beautiful.

    • First line: $.== 1 => 1/1 chance to keep this line1.
    • Second line: $. == 2 => 1/2 chance to keep line2 (so 1-(1/2)=1/2 to keep line1) (Equiprobability)
    • Third line: $. == 3 => 1/3 chance to keep line3 ( so (1-(1/3))=2/3 chance to keep a previous line (line1 or line2) : (2/3)*(1/2)=1/3 for line1 and same thing 1/3 for line2 (Equiprobability)
    • (N+1)th line: $. == N+1 => 1/(N+1) chance to keep line(N+1) so (N+1 -1)/(N+1) to keep a previous line, one of all previous N lines (each one has 1/N): for each previous line probability is ((N+1 -1)/(N+1))*(1/N) = 1/(N+1) (Equiprobability again)
Re^3: Opening random files then reading random lines from file.
by rovf (Priest) on Apr 30, 2012 at 08:42 UTC
    You are absolutely right; when I wrote my comment, I did not know yet the ingenious (and simple!) algorithm proposed by JavaFan.

    -- 
    Ronald Fischer <ynnor@mm.st>