in reply to 2 bit Math puzzle

You don't want to add 1 here:
sub Get_Target{ ###Gets two random primes a creates the goal. my $i = @primes; $i++; my $value1 = int(rand($i)); my $value2 = int(rand($i)); $goal = $primes[$value1] * $primes[$value2]; }
Remove the $i++ line. With it in there, you can end up attempt to fetch $primes[@primes], which returns undef.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: 2 bit Math puzzle
by sparkyichi (Deacon) on Dec 13, 2001 at 23:54 UTC
    I do because I want to include the last prime number in the randomization. If I don't it will go from 0 to lest numb but not including last numb.

    Sparky
      No, that's not true.
      @a = qw( a e i o u ); # indices are 0, 1, 2, 3, 4 $x = int rand @a; # $x is a random integer # st 0 <= $x < @a # where @a is 5 # thus, 0 <= $x < 5 # thus, $x is from 0 to 4
      If you add one, you run the risk of accessing an element that doesn't exist. Trust me.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        In the Perl Cook Book it states In chapter 2.7. Generating Random Numbers. "The rand function returns a fractional number, from (and including) 0 up to (but not including) its argument. We give it an argument of 51 to get a number that can be 0 or more, but never 51 or more." I take it that i meens if there are 25 elements to the ararry then the posible numbers are from 0 to 24.999999... the remainder get cut off.

        I see your right because the elements are 0..24 thanks Japhy. I will make the correction.

        Sparky