Re: rand()
- From: charley@xxxxxxxxxxxx (Chris Charley)
- Date: Mon, 28 Jul 2008 14:53:35 -0400
----- Original Message ----- From: "Bobby" <cybercruiserz@xxxxxxxxx>
Newsgroups: perl.beginners
To: <beginners@xxxxxxxx>
Sent: Monday, July 28, 2008 10:54 AM
Subject: rand()
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 can use 'shuffle' from List::Util.
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/ shuffle /;
my $max = 3;
my @nums = ("10000","10002","10004","10005","10006","140000","1500000");
my @winner = (shuffle @nums)[0 .. $max - 1];
print "@winner";
'shuffle' does what its name says - shuffles the list items. You will get no duplicates.
Chris
.
- Follow-Ups:
- Re: rand()
- From: Chris Charley
- Re: rand()
- References:
- rand()
- From: Bobby
- rand()
- Prev by Date: Re: rand()
- Next by Date: Re: rand()
- Previous by thread: Re: rand()
- Next by thread: Re: rand()
- Index(es):
Relevant Pages
|