I created a perl script that plays an online guessing game, the only problem is the...

Billie Dee

New member
The random number will always change. If you don't want your $ans to change - set it to a constant. Random is used when you want different numbers to be randomly chosen.

Dictionary.Com - Statistics. of or characterizing a process of selection in which each item of a set has an equal probability of being chosen.

You're choosing a random number ( think) between 0 and 100). What happens when your $ans = int(rand(100))+1 is 101?
 
...random number changes? every time the page is submitted, I know the web is stateless and that a <input type=hidden> can be used I just don't know where... I have written the code two separate ways and nothings working.. Here's what I have

#!/usr/local/bin/perl

use CGI qw/:standard/;

print header;
print start_html('A Guessing Game'),
h1('A Guessing Game'),
h3('Pick A Number From 1-100, If Your Right.. YOU WIN!'),
start_form,
"What's your Guess? ",textfield('num'),
p,
submit,
end_form,
hr;
if (param()) {
$num = param('num');
$ans = int(rand(100))+1;

if ($num== $ans)
{
print "YOU WIN!!! Random Number: $ans\n";
}
elsif($num > $ans)
{
print "Too high!\n";
}
else
{
print "Too low!\n",
hr;
}
}

print end_html;
 
you need to add the hidden field anywhere between start_form and end_form. Sorry I don't know the syntax. BUT then you have to worry about WHEN exactly do you generate the random number?
 
Back
Top