Re: rand()



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




Relevant Pages

  • Re: rand()
    ... 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 ... use warnings; ...
    (perl.beginners)
  • Re: rand()
    ... Newsgroups: perl.beginners ... How do I use the rand function to print out more than one random number for an array? ... You can use 'shuffle' from List::Util. ...
    (perl.beginners)
  • Re: Generate two random number and multiply
    ... To generate pseudo-random integer values, use the rand function. ... it's recommended to invoke the srand function to ... To select a string, from a collection randomly, you can use the same ... bounds of your array of strings and use it to index into the array and ...
    (comp.lang.c)
  • Re: rand()
    ... 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? ... You may need srand before the rand function. ...
    (perl.beginners)
  • Re: rand()
    ... In article, Bobby ... How do I use the rand function to print out more than one random number for ... an array? ... print "$randnum \n"; ...
    (perl.beginners)