Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Opening random files then reading random lines from file. by Marshall
in thread Opening random files then reading random lines from file. by fame

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found