Gossamer Forum
Home : General : Perl Programming :

check and save random number

Quote Reply
check and save random number
I'm try to make a program that will make a random number.
Then check a file to see if number is in that file. If number is in file then remake another random number and check again. If number isn't in file then save number in that file. And save number in another file.

So far I have
In Reply To:
#!/usr/bin/perl
my ($found, @values, @match);

open (DB, "<random_master.txt") or die "Unable to open.";
$found = 0;
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
chomp;
@values = &split_decode($_);

NUMBER:
$found = 0;
$b = 0;
@array=(1..10);
range 1 to 9 random number
$random = $array[rand @array];
$b = "$random";

if ($values[$num] eq $b) {
push @match,$_;
$found++;
print "no more numbers available \n";
goto NUMBER ;
} # end of if

} # end of while

close DB;
goto NUMBER if $values[$num] eq $b;


# If no matching record found, save number in both files
if ($found < 1) {
open(USER, ">>random_user.txt") or die "can't open USER \n";
print USER "$b\n";
close (USER);
# append to master file
open(MASTER, ">>random_master.txt") or die "can't open MASTER \n";
print MASTER "$b\n";
close (MASTER);
print "\n";
print $b;
print "\n";

exit;
} # end of if

print "\n";
print "\n";

sub split_decode {
# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.
$db_delim = '|';

my ($input) = shift;
my (@array) = split (/\Q$db_delim\E/o, $input, $#db_cols+1);
foreach (@array) {
s/~~/$db_delim/g; # Retrieve Delimiter..
s/``/\n/g; # Change '' back to newlines..
}
return @array;
}
so far it almost works. However when its suppose to remake random number because of a duplicate - it doesn't.

Any help pointing me in the right direction would be GREATLY appreciated.


Quote Reply
Re: check and save random number In reply to
never mind I figured it out.
I'm amazed as to how simple this was Blush