$minlength+=int(rand($maxlength))
should be
$minlength+int(rand($maxlength-$minlength))
You want a number in [0..$#vchars], but
int(rand($#vchars))
returns a number in [0..$#vchars-1]. You want
int(rand(@vchars))
The int() isn't needed here since array indexes are coerced into ints already.
my $cnt=1; while ($cnt <= $count) { ... $cnt++; }
would be cleaner as
for (1..$count) { ... }
Similarly for the inner loop.
@vchars only needs to be initialized once, not $count times.
use strict; use warnings; my $count = 10; my $minlength = 3; my $maxlength = 6; my @vchars = ('a'..'z', 'A'..'Z', 0..9); for (1 .. $count) { my $data = ''; for (1 .. $minlength+int(rand($maxlength-$minlength))) { $data .= $vchars[rand(@vchars)]; } print("$data\n"); }
In reply to Re: Creating random generated data...
by ikegami
in thread Creating random generated data...
by perl_junkie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |