PDA

View Full Version : Pick random number from a text files


poyor7
02-21-2007, 08:36 AM
Hi,

I'm trying to pick randomly number from a text files where inside a text files consist a bunch of number. My scenario is when user submit a form they will receive a special number randomly. But so far this is somehow i managed to do:


#rand_text.pl file
#Please change this -bcause i run in window
#!"C:\Program Files\xampp\perl\bin\perl.exe"
$file = "random.txt";
srand(time() ^ ($$+ ($$ << 10)) );
# $limit = 10;
$num = int(rand(10)+ 1);
open (INFILE,"$file");
$i = 0;
while ( $i < ($num) ){
$line = <INFILE>;
# print "$line";
$i++;

}
close (INFILE);
print <<EOH;
Content-type: text/html
<title>Click It!!</title>
<center>
<a href=\"rand_text.pl\">Again</a>
<P>
<BR>
<font size=+3>$line</font>
<P>
EOH


#random.txt
111
222
333
up until 10


So my question am i doing the right things?...Oh well the code is working but sometimes when i click or refresh, let say it pick number 111 then i refresh again it also pick 111 then another refresh it will pick anotehr number...can anyone assists me or at least some hints to solve or touch up the code..

Thanks,

Gypsy
02-21-2007, 01:50 PM
It sounds as if it is working. The fact that it's random means a number may present itself twice in a row.

KevinADC
02-21-2007, 09:33 PM
already answered on another forum too.