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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.