Re: rand()



In article <532078.84144.qm@xxxxxxxxxxxxxxxxxxxxxxxxxxx>, 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.
Ý
#!/usr/bin/perl
use strict;
use warnings;
my @nums = ("10000","10002","10004","10005","10006","140000","1500000");
my $randnum = $nums[rand(@nums)];
print "$randnum \n";

Use a loop:

for ( 1..5 ) [
my $randnum = $nums[rand(@nums)];
print "$randnum \n";
}

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • Re: rand()
    ... use warnings; ... print "$randnum \n"; ... for an array? ... You may need srand before the rand function. ...
    (perl.beginners)
  • 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: rand()
    ... use strict; ... use warnings; ... if the size of the array increases just as above the duplication chance reduced or disappear completely. ...
    (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)