Re: rand()
- From: cybercruiserz@xxxxxxxxx (Bobby)
- Date: Mon, 28 Jul 2008 13:17:57 -0700 (PDT)
That worked Shawn. Thanks.
I'm passing @nums values from an html form; so now my @nums = ($list) where $list = 97000,97005,98000,96100,94003 . The rand funtion now interprets $list as a string and not integers so the script doesn't works anymore. How do i convert values with that list from a string to itegers so that the script will work properly? Thanks for any suggestion.
--- On Mon, 7/28/08, Mr. Shawn H. Corey <shawnhcorey@xxxxxxxx> wrote:
From: Mr. Shawn H. Corey <shawnhcorey@xxxxxxxx>
Subject: Re: rand()
To: myklass@xxxxxxxxx
Cc: peng.kyo@xxxxxxxxx, cybercruiserz@xxxxxxxxx, beginners@xxxxxxxx
Date: Monday, July 28, 2008, 12:13 PM
On Mon, 2008-07-28 at 19:02 +0100, Aruna Goke wrote:
peng.kyo@xxxxxxxxx wrote:<cybercruiserz@xxxxxxxxx> wrote:
On Mon, Jul 28, 2008 at 11:52 PM, Bobby
numbers ofPeng,
Could you give me an example code? I want to randomly select X
select 3numbers from the @nums list. For instance, i want to randomly
srand to donumbers from @nums i.e. 10000, 10005, 140000. How would you use
("10000","10002","10004","10005","10006","140000","1500000");this?
Try the modified code below, it works fine.
use strict;
use warnings;
my $max=3;
my @nums =
("10000","10002","10004","10005","10006","140000",my @randnum = map { $nums[int rand(@nums)] } 1 .. $max;
print "@randnum \n";
#!/usr/bin/perl
use strict;
use warnings;
my $max = 3;
my @nums =
"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
Do you mean something like this?
#!/usr/bin/perl
my $max = shift @ARGV || 10;
my @nums = ( 10000, 10002, 10004, 10005, 10006, 140000, 1500000, 100011,
10001, 100014, 100015, 100016, 1400100, 15010000 );
my @randnum = ();
for my $count ( 1 .. $max ){
push @randnum, splice( @nums, rand( @nums ), 1 );
}
print "@randnum \n";
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
"Where there's duct tape, there's hope."
"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
- Follow-Ups:
- Re: rand()
- From: Rob Dixon
- Re: rand()
- From: Mr. Shawn H. Corey
- Re: rand()
- References:
- Re: rand()
- From: Mr. Shawn H. Corey
- 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
|