in reply to Re: 2 bit Math puzzle
in thread 2 bit Math puzzle

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

Replies are listed 'Best First'.
Re: Re: Re: 2 bit Math puzzle
by japhy (Canon) on Dec 13, 2001 at 23:59 UTC
    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