Jump to content
Sign in to follow this  

Perl, random unique numbers

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

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...