perl.beginners
[Top] [All Lists]

Re: rand()

Subject: Re: rand()
From: cybercruiserz@xxxxxxxxx (Bobby)
Date: Mon, 28 Jul 2008 08:52:35 -0700 (PDT)
Newsgroups: perl.beginners
Peng,
 
Could you give me an example code? I want to randomly select X numbers of 
numbers from the @nums list. For instance, i want to randomly select 3 numbers 
from @nums i.e.  10000, 10005, 140000. How would you use srand to do this? 
 
Would Math::TrulyRandom be a better solution since i want to use this function 
to randomly pick winners for a drawing contest. If so could some one kindly 
give me some example codes? Thanks.  
 
#!/usr/bin/perl
use strict;
use warnings;
my @nums = ("10000","10002","10004","10005","10006","140000","1500000");
my $randnum = $nums[rand(@nums)];
print "$randnum \n";



--- On Mon, 7/28/08, peng.kyo@xxxxxxxxx <peng.kyo@xxxxxxxxx> wrote:

From: peng.kyo@xxxxxxxxx <peng.kyo@xxxxxxxxx>
Subject: Re: rand()
To: cybercruiserz@xxxxxxxxx
Cc: beginners@xxxxxxxx
Date: Monday, July 28, 2008, 9:06 AM

On Mon, Jul 28, 2008 at 10:54 PM, Bobby <cybercruiserz@xxxxxxxxx> wrote:
> Hi all,
>
> How do I use the rand function to print out more than one random number
for an array? In the example script below i have an array @nums with a list of
numbers; how do i print out more than one random numbers from that list
(@nums)? Thanks.
>

You may need srand before the rand function.



-- 
Regards,
Jeff. - Peng.Kyo@xxxxxxxxx


      
<Prev in Thread] Current Thread [Next in Thread>