Jump to content

Perl, random unique numbers


racecar

Recommended Posts

Hi, I wrote a script that will generate random unique numbers that will store in a text file.When I run it over and over, It only generate 90% of unique numbers. What happen to the 10%? Can you please explain. Thank you very much.

#!/usr/bin/perlsub mains {my $sov=random();my $red=readFile("lognum");chomp($red, $sov);  if($red!~/^($sov)/gm){   writeFile($sov);   return 1;  } else {  return 0;  }}sub random{    my $num=int(rand()*1000);  return $num;}sub readFile{  my $infile=shift;  my $lines;    # Provide local separator    local($/, *FILE);    $/=undef;           open(FILE, $infile) || die "Cannot open file $infile";    $lines=<FILE>;    close(FILE);  return $lines;}sub writeFile{    my $outfile=shift;    my $lFile="lognum";    open(OUT,">>$lFile") || die "Cannot open file $lFile";      print OUT $outfile."\n";    close(OUT);}while(1){ mains(); }

Edited by racecar
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...