Re: rand()
- From: myklass@xxxxxxxxx (Aruna Goke)
- Date: Mon, 28 Jul 2008 19:02:31 +0100
peng.kyo@xxxxxxxxx wrote:
On Mon, Jul 28, 2008 at 11:52 PM, Bobby <cybercruiserz@xxxxxxxxx> wrote: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?
Try the modified code below, it works fine.
use strict;
use warnings;
my $max=3;
my @nums = ("10000","10002","10004","10005","10006","140000","1500000");
my @randnum = map { $nums[int rand(@nums)] } 1 .. $max;
print "@randnum \n";
#!/usr/bin/perl
use strict;
use warnings;
my $max = 3;
my @nums = ("10000","10002","10004","10005","10006","140000",
"1500000",100011, 10001, "100014", "100015", "100016","1400100", "15010000");
my @randnum = map { $nums[int rand(@nums)] } 1 .. $max;
print "@randnum \n";
The code supplied by peng is right.
you had duplicates because it was run over a small array.. if the size of the array increases just as above the duplication chance reduced or disappear completely.
Thanks
.
- Follow-Ups:
- Re: rand()
- From: Mr. Shawn H. Corey
- Re: rand()
- References:
- Re: rand()
- From: Peng Kyo
- Re: rand()
- From: Bobby
- Re: rand()
- From: Peng Kyo
- Re: 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
|