I'd probably rewrite this way:
use strict;
use warnings;
my $records_wanted = 10;
my $minlength=3;
my $maxlength=6; # max length of the record - will be specified by use
+r
my @alphabet = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9' );
my $records_made = 0;
while ( $records_made++ < $records_wanted ) {
my $record = '';
my $record_length
= $minlength + int rand( $maxlength - $minlength + 1 );
warn "rlength: $record_length\n";
while ( length $record < $record_length ) {
$record .= $alphabet[ rand @alphabet ];
}
warn "record: $record\n";
}
A few notes...
- Randomly selecting an element from some array is pretty easy ($foo[ rand @foo ]), and $#foo is not the same as scalar @foo.
- If you name your variables better (e.g., $records_wanted, $records_made), you need fewer comments.
- This modifies $minlength: "$rlength=$minlength+=int(rand($maxlength))".
- If you use warn for your debugging output, it's easier to find later when it's time to remove them.
Sample output:
rlength: 4
record: 3J9J
rlength: 5
record: 9NxuW
rlength: 5
record: rwtwj
rlength: 3
record: AVY
rlength: 3
record: WPY
rlength: 5
record: wxeTn
rlength: 5
record: 86JH1
rlength: 6
record: PwLx2X
rlength: 4
record: nXuG
rlength: 4
record: cpXk
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.