### Re^2: Opening random files then reading random lines from file.

by choroba (Prior)
 on Apr 27, 2012 at 08:39 UTC ( #967552=note: print w/ replies, xml ) Need Help??

If I correctly understand the

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

Comment on Re^2: Opening random files then reading random lines from file.
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>

Create A New User
Node Status?
node history
Node Type: note [id://967552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2013-06-19 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?