Chas Owens wrote:
> On 3/16/07, Grant <[EMAIL PROTECTED]> wrote:
>> Hello, I need to generate two random numbers.  One should be a 1, 2,
>> or 3, and the other should be a 1 or 2.  How can I do that?
> 
> That depends on your needs.  The rand function creates decent quality
> pseudo-random numbers (it calls srand with the time if it has not
> already been called) greater than  0 and less than 1.  Therefore you
> can get a random 1, 2, or 3 like this
> 
> my $num = int rand 3 + 1;

That will return either 0, 1, 2 or 3.  The addition has higher precedence.


> Likewise you can get the other like this:
> 
> my $num = int rand 2 + 1;

That will return either 0, 1 or 2.




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to