I am getting output but I'm getting 300 lines that are exactly the same.
while (<IN>){ while ($count<=$size){ rand($.)<1 && ($line=$_); print OUT $line; $count++; } }
Of course you are.

For each line in the file, $_ has a specific value. After that, for 300 times, you decide, based on a random number, to assign this one value to $line. Always the exact same string. And then you print it out.

Whether you assign a value to a variable or not to a variable that already has been set to this value, it doesn't change a thing.

So, where's your thinko... It's quite obvious to me where you got the basis for the algorithm, it is (or used to be) in the official Perl FAQ. It goes something like this:

while (<IN>){ rand($.)<1 && ($line=$_); } print OUT $line;
So you loop through the file, assign or don't assign the current value to $line based on a random value, and in the end, you print out what you have got.

If you insist to do this 300 times, you will have to read through the file 300 times.

If you don't want to do that, and you've got memory to spare, you can first read the contents of the file into an array, and randomly pick a line from that array.

Using the same algorithm (for no good reason, it was chosen because it works without keeping everything in memory at once), this becomes:

my @lines = <IN>; for my $c (1 .. 300) { my $line; for my $i (0 .. $#lines) { rnd($i+1)<1 and $line = $#lines[$i]; } print OUT $line; }
but it'll be a lot shorter to just write
my @lines = <IN>; for my $c (1 .. 300) { print OUT $lines[int rand @lines]; }

That leaves in duplicates. If you don't want duplicates, simply import the shuffle function from List::Util, shuffle the lines array, and print out the first 300.

use List::Util qw(shuffle); my @lines = shuffle(<IN>); print OUT @lines[0 .. 299];
This assumes there are at least 300 lines in the file, or you'll get a bunch of undefined values at the end.

In reply to Re: Picking Random Lines from a File by bart
in thread Picking Random Lines from a File by de2425

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.