$seed and $rand are indexes into @range, but @range changes so you need to get the value at that index.

use strict; use warnings; my $low = 1; my $high = 10; my @range=($low .. $high); my $rand=$range[rand(@range)]; # <--- my $seed=$low-1; # <--- Works for all values of $low + and $high. my $count=1; my $lastguess; while ( $seed != $rand ) { #undef @range if ( $ count > 1); # <--- Useless. @range=($low .. $high); $lastguess = $seed; $seed = $range[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";

Added: As a bonus, here's cleaner code and output:

use strict; use warnings; my $low = 1; my $high = 10; my $range = $high - $low + 1; my $rand = $low + int(rand($range)); my $count = 0; for (;;) { my $guess = $low + int(rand($range)); $count++; print("$count) Guessing $guess."); last if $guess == $rand; if ( $guess < $rand ) { print " Too low.\n"; $low = $guess + 1; } elsif ( $guess > $rand ) { print " Too high.\n"; $high = $guess - 1; } $range = $high - $low + 1; } if ($count == 1) { print " Got it on the first try!\n"; } else { print " Got it in $count tries.\n"; }

It's "cleaner" because (1) a list (@range) isn't built where one isn't needed, (2) $count is updated when a guess is actually made, (3) $seed is more appropriately called $guess, and (4) $seed isn't checked against $guess until a guess is actually made. Also, (5) I removed the previous guess from the output, since (A) it would need to be omitted on the first guess, and (B) the previous guess is already displayed on the previous line.


In reply to Re: generating random numbers within a range by ikegami
in thread generating random numbers within a range by tcf03

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.