Hi fellow Perlmonks!
I'm yet another newbie to PERL and already I love it.
My question is one of verifying the below.
I've written a simple PERL program to generate and print out an array of 6 Million unique codes and used grep as a filter to remove any potential duplicates from the initial array. So below is the code.
Incidentally, I tried to install and use the Mersenne Twister module on CPAN, for the random generation, but the 'make' kept failing when trying to install it - my fault.
So it might be obvious, but am I correct in thinking that the below will result in a truly unique code set? I'm asking as my client might like a second opinion from seasoned PERL divers, Thanks!
Rich.
#!/usr/bin/
#program to generate 6 Million unique codes, Oct 15, 2011.
open(MYOUTFILE, ">codesSixMil.txt"); #open for write, overwrite (>)
$count = 0; #keep track of how many codes generated
$count1 = 0; #keep track of how many codes duplicated
$desiredQuantity = 6000001;
@arrayOfResults =();
sub generate_random_string {
my $stringsize = shift;
my @alphanumeric = ('a'..'z', 3,4,6,7,9);
my $randstring = join '', (map { $alphanumeric[rand@alphanumeric] }
+ @alphanumeric)[0 .. $stringsize];
return $randstring;
}
for ($i=0;$i<$desiredQuantity;$i++) {
$returnvalue = generate_random_string(10);
push(@arrayOfResults, $returnvalue);
$count++;
}
#remove any potential duplicates
@unique = grep { ++$count{$_} < 2 } @arrayOfResults;
#loop through array to output each code on its own line
#with a carriage return as required.
for ($i=0;$i<$desiredQuantity;$i++) {
print @unique[$i], "\n";
print MYOUTFILE @unique[$i], "\r\n";
$count1++;
}
#do some rudimentary checking and output result.
print "\n---Code generation report--- \n\n";
print "First array: $count \n";
print " Unique array: $count1 \n\n";
print "---There are ", $count-$count1, " code duplicates in this listi
+ng of ", $desiredQuantity, " ---\n\n\n";
#*** Close the file ***
close(MYOUTFILE);
I dove into the C, found a Shell and inside was a PERL.
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.