Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

if condition only returns false . . .

by ashaman (Sexton)
on Apr 05, 2001 at 04:08 UTC ( [id://69948]=perlquestion: print w/replies, xml ) Need Help??

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://69948]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found