in reply to Guess That Number

my $low = 1; #Current low limit my $high = 1000; #Current high limit #This is the secret number...Add one because we #don't need to wind up with a zero now do we? my $goal = int(rand($high))+1;

That's only correct if $low = 1. The general correct algorithm would be:

my $goal = int(rand($high-$low+1))+$low;

Update: Since you seem to be having trouble proving to yourself that your algorithm is broken while mine is not, I offer here a test script and sample output.

my $low = 7; my $high = 9; sub original { int(rand($high))+1 } sub fixed { int(rand($high-$low+1))+$low } ($,,$\)=(" ","\n"); print "Random numbers between $low and $high."; print "original:", map original(), 1..20; print " fixed:", map fixed(), 1..20;
Random numbers between 7 and 9. original: 9 7 1 5 3 9 9 6 7 1 3 5 2 4 7 6 7 3 4 8 fixed: 9 7 9 7 7 8 7 9 7 7 8 8 9 7 8 8 7 9 8 9
Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.

Replies are listed 'Best First'.
Re^2: Guess That Number
by naildownx (Beadle) on Jul 09, 2009 at 19:24 UTC
    The change you gave me jdporter worked fine...but now when I try to change the d to a D it keeps giving me Please enter a number only...
    # jdporter's change...worked fine my $goal = int(rand($high-$low+1))+$low; while (1) { print "Enter a number between $low and $high: "; #The answer from the user my $answer = <STDIN>; chomp($answer); # when I change the d to a D I get "Please enter a number only" if ($answer !~ /\d+/) { print "Please enter a number only\n"; next;
    The early bird gets the worm but the second mouse gets the cheese.
    pretendeavor

      Please do not ignore BioLion's reply, below. He is correct, as is toolic. You are not looking closely enough. Could be Perl's fault for being so cryptic and terse, or it could be your fault for making assumptions about what the code is doing.