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

Some "simple" solutions for you.
Be aware that the Perl rand() function is not completely random - works ok for most situations but is not really "random" - there are biases in this (odd vs even), etc.

If you want to use rand() to plan Casino strategies, there are better number generators!

But this is one way to get the job done. I do recommend buying a copy of the Perl Cookbook.

#!/usr/bin/perl -w use strict; #pick a "random arg from a list" my @names = qw(ny.txt ca.txt nj.txt pa.txt ma.txt wa.txt); my $random_file = $names[rand(@names)]; #UPDATE: previous code had #an "off-by-one" error, Oops! print "$random_file\n";
Pick A Random Line from a file:
From: Perl Cookbook
#Chapter 8.6. Picking a Random Line from a File #Problem: You want to return a random line from a file. #Use rand and $. (the current line number) to decide #which line to print: srand; rand($.) < 1 && ($line = $_) while <>;
Exactly how this works is complicated.
The "Cookbook" is a great book and highly recommended by me.

Replies are listed 'Best First'.
Re^2: Opening random files then reading random lines from file.
by jwkrahn (Abbot) on Apr 27, 2012 at 06:24 UTC
    my $random_file = $names[rand(@names-1)];

    That will always skip the last element of @names.    It should be:

    my $random_file = $names[ rand @names ];
      Correct.

      These pesky "off-by-one" errors (the most common error in programming) are indeed annoying! But yes, you are correct! Thanks!

      previous post updated with comment.

Re^2: Opening random files then reading random lines from file.
by JavaFan (Canon) on Apr 27, 2012 at 09:18 UTC
    Exactly how this works is complicated.
    No, it isn't. How it works is as follows: we read a file, line by line. For the kth line read, we decide with probability 1/k whether we remember the line, forgetting any previous one.

    That's fairly trivial. It takes just a little analysis to see it's fair. Fair means that any line will be the final pick. So, what's the probability of line k to be the final pick? First what needs to be happen is that it gets remembered when the line is read, which has a probability of 1/k. Then, any next lines must *not* be picked. For each of the next lines, this happens with probability (j -1)/j, where j is the line number. Since all those probabilities are independent of each other, we can multiply them:

               n
            -------
        1    |   |    j-1        1     k      1 
       ---   |   |    ---   ==  --- * --- == ---
        k    |   |     j         k     n      n
             j=k+1
    
    Too bad we can't really do math in HTML.
      I think you made my point that it is "complicated"
      (not easily explained and understood)..
        You and I must have a wildly different idea what is complicated.

        If you find a one-liner using a bog standard file iteration loop complicating, I wonder what you think of sort.