tcf03 has asked for the wisdom of the Perl Monks concerning the following question:
Im just not seeing what is wrong. any help is appreciateduse strict; my $low = 1; my $high = 10; my @range=($low .. $high); my $rand=int(rand(@range)); my $seed=-1; my $count=1; my $lastguess; while ( $seed != $rand ) { undef @range if ( $ count > 1); @range=($low .. $high); $lastguess = $seed; $seed = int(rand(@range)); if ( $seed < $rand ) { print "You're too low - $seed (last guess = $lastguess)\n"; $low=$seed+1; $count++; } elsif ( $seed > $rand ) { print "you're too high - $seed (last guess = $lastguess)\n"; $high=$seed-1; $count++; } } print "you got it in $count tries\n"; print "seed = $seed rand = $rand ( last guess = $lastguess )\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: generating random numbers within a range
by moot (Chaplain) on May 09, 2005 at 19:34 UTC | |
by xdg (Monsignor) on May 09, 2005 at 21:01 UTC | |
Re: generating random numbers within a range
by ikegami (Patriarch) on May 09, 2005 at 19:34 UTC | |
by tcf03 (Deacon) on May 09, 2005 at 19:40 UTC | |
by ikegami (Patriarch) on May 09, 2005 at 20:36 UTC | |
by tcf03 (Deacon) on May 09, 2005 at 20:40 UTC | |
Re: generating random numbers within a range
by cees (Curate) on May 09, 2005 at 19:47 UTC |