perl_junkie has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am working on a random data generation program which accepts the data type and data length as input parameters and then creates random data for this, so that it can be loaded into the tables for testing. I got code pieces from another thread and what I have pasted below gives an alphanumeric output by taking the number of records and the length as inputs. But I am having the hardest time getting a proper alphanumberic output from this if the length of the data and the number of records are given.

Can someone please help me out with this?

Thanks!

#!/usr/bin/perl use Fcntl; #The Module use strict; use Math::Trig; my $count=10; # Number of records required. my $minlength=3; my $maxlength=6; # max length of the record - will be specified by use +r my $rlength; my $cnt=1; while ($cnt <= $count) { my $newvar; my @vchars=('a'..'z','A'..'Z',0..9); $rlength=$minlength+=int(rand($maxlength)); print "rlength is $rlength\n"; while(length($newvar) < $rlength) { my $char=$vchars[int(rand($#vchars))]; $newvar.=$char; } $cnt++ }

Replies are listed 'Best First'.
Re: Creating random generated data...
by kyle (Abbot) on Mar 25, 2008 at 17:42 UTC

    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:

      Thanks Ikegami, Kyle....!!! This really helps.

      Kyle, I have noted your comments and will keep them in mind.... I have only been doing this for a month and the inexperience probably shows..!! :)

Re: Creating random generated data...
by ikegami (Patriarch) on Mar 25, 2008 at 17:27 UTC
    • $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"); }
Re: Creating random generated data...
by planetscape (Chancellor) on Mar 25, 2008 at 21:57 UTC