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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Guess That Number
by naildownx (Beadle) on Jul 09, 2009 at 19:24 UTC | |
by jdporter (Paladin) on Jul 09, 2009 at 19:29 UTC |