ashaman has asked for the wisdom of the Perl Monks concerning the following question:

i'm like, brand new to perl, so if my question seems a little simple, well . . . that's to be expected. anyway. i'm working on a script at the moment, and for some reason my if statement only returns false.
if(int(rand()) == 1) { $onedig = &Alpha} else {$onedig = int(rand(9))};

it always ends up running the else block and never the if block. can anyone tell me what's going wrong?

Replies are listed 'Best First'.
Re: if condition only returns false . . .
by AgentM (Curate) on Apr 05, 2001 at 04:14 UTC
    Dr. doc sez: Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value 1 is used.

    Since a decimal number 0<x<is returned and concatenated, 0 takes the place of the left side of the comparison and 0 never equals 1. Perhaps you'd like to provide an argument?

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: if condition only returns false . . .
by ton (Friar) on Apr 05, 2001 at 04:15 UTC
    rand returns a floating point number between 0 and 1. The int operator takes the integer portion of that number, which will always be zero for numbers in the range [0, 1). Hence, always false!