[31705] in Perl-Users-Digest
Perl-Users Digest, Issue: 2968 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 1 06:10:57 2010
Date: Tue, 1 Jun 2010 03:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 1 Jun 2010 Volume: 11 Number: 2968
Today's topics:
How to generate random number without replacement? <pengyu.ut@gmail.com>
Re: How to generate random number without replacement? (Randal L. Schwartz)
Re: How to generate random number without replacement? <pengyu.ut@gmail.com>
Re: How to generate random number without replacement? <jurgenex@hotmail.com>
Re: How to generate random number without replacement? <uri@StemSystems.com>
Re: How to generate random number without replacement? <pengyu.ut@gmail.com>
Re: How to generate random number without replacement? <jurgenex@hotmail.com>
Re: How to generate random number without replacement? <uri@StemSystems.com>
Re: How to generate random number without replacement? <pengyu.ut@gmail.com>
Re: How to generate random number without replacement? <uri@StemSystems.com>
How to take two input streams? <pengyu.ut@gmail.com>
Re: How to take two input streams? (Alan Curry)
Re: How to take two input streams? <m@rtij.nl.invlalid>
Re: How to take two input streams? <pengyu.ut@gmail.com>
Re: How to take two input streams? <pengyu.ut@gmail.com>
Re: How to take two input streams? <peter@makholm.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 31 May 2010 19:43:08 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: How to generate random number without replacement?
Message-Id: <20cfc406-5c57-4e35-adc2-96f72d94db0a@v18g2000vbc.googlegroups.com>
It seems that the int(rand(10)) generate random with replacement. I'm
wondering how to generate random number without replacement in perl.
Is there a function for it?
------------------------------
Date: Mon, 31 May 2010 19:48:57 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to generate random number without replacement?
Message-Id: <86vda3a2na.fsf@red.stonehenge.com>
>>>>> "Peng" == Peng Yu <pengyu.ut@gmail.com> writes:
Peng> It seems that the int(rand(10)) generate random with replacement. I'm
Peng> wondering how to generate random number without replacement in perl.
Peng> Is there a function for it?
$ perldoc -q shuffle
Found in /usr/local/lib/perl5/5.10.1/pod/perlfaq4.pod
How do I shuffle an array randomly?
If you either have Perl 5.8.0 or later installed, or if you have
Scalar-List-Utils 1.03 or later installed, you can say:
use List::Util 'shuffle';
@shuffled = shuffle(@list);
If not, you can use a Fisher-Yates shuffle.
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
return unless @$deck; # must not be empty!
my $i = @$deck;
while (--$i) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}
# shuffle my mpeg collection
#
my @mpeg = <audio/*/*.mp3>;
fisher_yates_shuffle( \@mpeg ); # randomize @mpeg in place
print @mpeg;
Note that the above implementation shuffles an array in place, unlike
the "List::Util::shuffle()" which takes a list and returns a new
shuffled list.
You've probably seen shuffling algorithms that work using splice,
randomly picking another element to swap the current element with
srand;
@new = ();
@old = 1 .. 10; # just a demo
while (@old) {
push(@new, splice(@old, rand @old, 1));
}
This is bad because splice is already O(N), and since you do it N times,
you just invented a quadratic algorithm; that is, O(N**2). This does not
scale, although Perl is so efficient that you probably won't notice this
until you have rather largish arrays.
print "Just another Perl hacker,"; # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Mon, 31 May 2010 20:20:33 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <a2fc36dc-7920-4518-aa87-1481344e3682@y12g2000vbg.googlegroups.com>
On May 31, 9:48=A0pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "Peng" =3D=3D Peng Yu <pengyu...@gmail.com> writes:
>
> Peng> It seems that the int(rand(10)) generate random with replacement. I=
'm
> Peng> wondering how to generate random number without replacement in perl=
.
> Peng> Is there a function for it?
>
> $ perldoc -q shuffle
I don't really need shuffle. For example, if I want to sample 1000
number (without replacement) from the numbers between 1 and
1000,000,000, shuffle will take more runtime than necessary.
------------------------------
Date: Mon, 31 May 2010 20:25:07 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <p6v806l87i48s570lcsgecamv5vkrr62um@4ax.com>
Peng Yu <pengyu.ut@gmail.com> wrote:
>It seems that the int(rand(10)) generate random with replacement. I'm
>wondering how to generate random number without replacement in perl.
Could you please explain what you mean by "with/without replacement"?
A number is a number, it doesn't replace anything....
jue
------------------------------
Date: Mon, 31 May 2010 23:44:58 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <87pr0bs9fp.fsf@quad.sysarch.com>
>>>>> "PY" == Peng Yu <pengyu.ut@gmail.com> writes:
PY> On May 31, 9:48 pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
>> >>>>> "Peng" == Peng Yu <pengyu...@gmail.com> writes:
>>
Peng> It seems that the int(rand(10)) generate random with replacement. I'm
Peng> wondering how to generate random number without replacement in perl.
Peng> Is there a function for it?
>>
>> $ perldoc -q shuffle
PY> I don't really need shuffle. For example, if I want to sample 1000
PY> number (without replacement) from the numbers between 1 and
PY> 1000,000,000, shuffle will take more runtime than necessary.
then just call rand but cache your hits with a hash. if found in the
hash, then try again. this will work if your sample size of 1k is much
lower than the large number. it will still work but be slow if the
sampling size is close to the total size.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Mon, 31 May 2010 20:45:36 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <bce39152-47d4-48c6-8351-cc14bfba9cd6@c7g2000vbc.googlegroups.com>
On May 31, 10:25=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> Peng Yu <pengyu...@gmail.com> wrote:
> >It seems that the int(rand(10)) generate random with replacement. I'm
> >wondering how to generate random number without replacement in perl.
>
> Could you please explain what you mean by "with/without replacement"?
> A number is a number, it doesn't replace anything....
These are standard concepts in statistics. Please see the following
webpage for the explanations on sampling 'with/without replacement'.
http://www.ma.utexas.edu/users/parker/sampling/repl.htm
------------------------------
Date: Mon, 31 May 2010 21:31:10 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <o53906ditbqhe7vplg2uukm0qdbf27gd0q@4ax.com>
Peng Yu <pengyu.ut@gmail.com> wrote:
>On May 31, 10:25 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
>> Peng Yu <pengyu...@gmail.com> wrote:
>> >It seems that the int(rand(10)) generate random with replacement. I'm
>> >wondering how to generate random number without replacement in perl.
>>
>> Could you please explain what you mean by "with/without replacement"?
>> A number is a number, it doesn't replace anything....
>
>These are standard concepts in statistics. Please see the following
>webpage for the explanations on sampling 'with/without replacement'.
>
>http://www.ma.utexas.edu/users/parker/sampling/repl.htm
Ok. For those like me not familiar with this term: he means random
numbers with and without repetition.
jue
------------------------------
Date: Tue, 01 Jun 2010 01:03:18 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <874ohns5t5.fsf@quad.sysarch.com>
>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
JE> Peng Yu <pengyu.ut@gmail.com> wrote:
>> On May 31, 10:25 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
>>> Peng Yu <pengyu...@gmail.com> wrote:
>>> >It seems that the int(rand(10)) generate random with replacement. I'm
>>> >wondering how to generate random number without replacement in perl.
>>>
>>> Could you please explain what you mean by "with/without replacement"?
>>> A number is a number, it doesn't replace anything....
>>
>> These are standard concepts in statistics. Please see the following
>> webpage for the explanations on sampling 'with/without replacement'.
>>
>> http://www.ma.utexas.edu/users/parker/sampling/repl.htm
JE> Ok. For those like me not familiar with this term: he means random
JE> numbers with and without repetition.
and i told him how to do it. i won't tell him again. it is a simple
problem and hashes solve it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 1 Jun 2010 01:28:34 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <1f5f33b5-7514-47a8-af34-24b8c2d6362d@l6g2000vbo.googlegroups.com>
On Jun 1, 12:03=A0am, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "JE" =3D=3D J=FCrgen Exner <jurge...@hotmail.com> writes:
>
> =A0 JE> Peng Yu <pengyu...@gmail.com> wrote:
> =A0 >> On May 31, 10:25=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote=
:
> =A0 >>> Peng Yu <pengyu...@gmail.com> wrote:
> =A0 >>> >It seems that the int(rand(10)) generate random with replacement=
. I'm
> =A0 >>> >wondering how to generate random number without replacement in p=
erl.
> =A0 >>>
> =A0 >>> Could you please explain what you mean by "with/without replaceme=
nt"?
> =A0 >>> A number is a number, it doesn't replace anything....
> =A0 >>
> =A0 >> These are standard concepts in statistics. Please see the followin=
g
> =A0 >> webpage for the explanations on sampling 'with/without replacement=
'.
> =A0 >>
> =A0 >>http://www.ma.utexas.edu/users/parker/sampling/repl.htm
>
> =A0 JE> Ok. For those like me not familiar with this term: he means rando=
m
> =A0 JE> numbers with and without repetition.
>
> and i told him how to do it. i won't tell him again. it is a simple
> problem and hashes solve it.
What do you mean? I didn't ask you to tell me again.
But I feel sorry that perl doesn't provide such a function out of the
box.
------------------------------
Date: Tue, 01 Jun 2010 04:50:36 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to generate random number without replacement?
Message-Id: <87wrujp25f.fsf@quad.sysarch.com>
>>>>> "PY" == Peng Yu <pengyu.ut@gmail.com> writes:
PY> On Jun 1, 12:03 am, "Uri Guttman" <u...@StemSystems.com> wrote:
>> JE> Ok. For those like me not familiar with this term: he means random
>> JE> numbers with and without repetition.
>>
>> and i told him how to do it. i won't tell him again. it is a simple
>> problem and hashes solve it.
PY> What do you mean? I didn't ask you to tell me again.
i told you how to do it. either you didn't read it or you didn't get the
solution.
PY> But I feel sorry that perl doesn't provide such a function out of the
PY> box.
i feel sorry for you that you can't code this up in 5 minutes. it is
trivial to do as i outlined. name another lang that has this built in.
it isn't needed as it is easily made from a hash and a loop and calls to
rand. this is about 2 lines of code and possibly 1 line. here, i will
code it up on the fly and possibly even get it right. i leave making
into a sub as your exercise.
my %seen ;
while( 1 ) { $x = int rand( 100_000_000 ) ; $seen{$x} and next ;
$seen{$x} = 1; print $x }
oops, it wrapped into 3 lines.
was that too complex? it will print numbers until it runs out of
them. no duplicates. make it a for loop to control the number of
prints. can you handle that? do you feel sorry for perl now? show me
another lang that could do that as easily.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Mon, 31 May 2010 20:47:22 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: How to take two input streams?
Message-Id: <d31764b8-cd92-47d0-a0af-39292fede94d@f13g2000vbm.googlegroups.com>
diff can take two input streams in the following example (if my
interpretation is correct).
diff <(gunzip <a.gz) <(gunzip b.gz)
I'm wondering how to take two streams in a perl program.
------------------------------
Date: Tue, 1 Jun 2010 04:12:26 +0000 (UTC)
From: pacman@kosh.dhis.org (Alan Curry)
Subject: Re: How to take two input streams?
Message-Id: <hu21ba$sl9$1@speranza.aioe.org>
In article <d31764b8-cd92-47d0-a0af-39292fede94d@f13g2000vbm.googlegroups.com>,
Peng Yu <pengyu.ut@gmail.com> wrote:
>diff can take two input streams in the following example (if my
>interpretation is correct).
diff *requires* two input streams. With only one, it's hard to figure what it
would do.
>
>diff <(gunzip <a.gz) <(gunzip b.gz)
>
>I'm wondering how to take two streams in a perl program.
The <(cmd) syntax is handled by your shell, which substitutes a filename
which may be opened to gain access to the stream. diff doesn't know anything
abou it. diff just finds 2 filenames in argv, opens them, and reads them. You
can do the same in any language, including perl.
--
Alan Curry
------------------------------
Date: Tue, 1 Jun 2010 10:24:03 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: How to take two input streams?
Message-Id: <3msed7-s3m.ln1@news.rtij.nl>
On Mon, 31 May 2010 20:47:22 -0700, Peng Yu wrote:
> diff can take two input streams in the following example (if my
> interpretation is correct).
>
> diff <(gunzip <a.gz) <(gunzip b.gz)
>
> I'm wondering how to take two streams in a perl program.
This has nothing to do with diff or with perl, it's a function of your
shell. So it works the same for diff as for perl.
HTH,
M4
------------------------------
Date: Tue, 1 Jun 2010 02:16:25 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to take two input streams?
Message-Id: <968d679f-1053-414c-aca8-e5f497d26c52@c13g2000vbr.googlegroups.com>
On Jun 1, 3:24=A0am, Martijn Lievaart <m...@rtij.nl.invlalid> wrote:
> On Mon, 31 May 2010 20:47:22 -0700, Peng Yu wrote:
> > diff can take two input streams in the following example (if my
> > interpretation is correct).
>
> > diff <(gunzip <a.gz) <(gunzip b.gz)
>
> > I'm wondering how to take two streams in a perl program.
>
> This has nothing to do with diff or with perl, it's a function of your
> shell. So it works the same for diff as for perl.
I don't quite understand how this works. Would you please write a
small perl program which can print the two streams (with the following
command) to help me understand it?
example.pl <(cat a.txt) <(cat b.txt)
------------------------------
Date: Tue, 1 Jun 2010 02:26:59 -0700 (PDT)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: Re: How to take two input streams?
Message-Id: <287d5565-3823-45e7-8a0c-7a210ec5bdbb@y21g2000vba.googlegroups.com>
On Jun 1, 3:24=A0am, Martijn Lievaart <m...@rtij.nl.invlalid> wrote:
> On Mon, 31 May 2010 20:47:22 -0700, Peng Yu wrote:
> > diff can take two input streams in the following example (if my
> > interpretation is correct).
>
> > diff <(gunzip <a.gz) <(gunzip b.gz)
>
> > I'm wondering how to take two streams in a perl program.
>
> This has nothing to do with diff or with perl, it's a function of your
> shell. So it works the same for diff as for perl.
I think that I understand what you mean. <(cmd) is just like a
filename, right?
$ cat main.pl
#!/usr/bin/env perl
use warnings;
open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);
while(<IN1>) {
print
}
print "------\n";
while(<IN2>) {
print
}
$ ./main.pl <(cat main.pl) <(cat main.pl)
#!/usr/bin/env perl
use warnings;
open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);
while(<IN1>) {
print
}
print "------\n";
while(<IN2>) {
print
}
------
#!/usr/bin/env perl
use warnings;
open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);
while(<IN1>) {
print
}
print "------\n";
while(<IN2>) {
print
}
------------------------------
Date: Tue, 01 Jun 2010 11:40:59 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: How to take two input streams?
Message-Id: <87wrujw0no.fsf@vps1.hacking.dk>
Peng Yu <pengyu.ut@gmail.com> writes:
> I don't quite understand how this works. Would you please write a
> small perl program which can print the two streams (with the following
> command) to help me understand it?
>
> example.pl <(cat a.txt) <(cat b.txt)
makholm@makholm:/tmp$ perl ./example.pl <(cat example.pl) <(tr 'a-zA-Z' 'n-za-mN-ZA-M' < example.pl)
001: #!/usr/bin/perl
001:
001: use strict;
001: use warnings;
001:
001: my $fileno;
001: for my $file (@ARGV) {
001: $fileno++;
001: open my $fh, "<", $file
001: or next;
001:
001: printf "%03d: %s", $fileno, $_ while <$fh>;
001: }
001:
001: __END__
002: #!/hfe/ova/crey
002:
002: hfr fgevpg;
002: hfr jneavatf;
002:
002: zl $svyrab;
002: sbe zl $svyr (@NETI) {
002: $svyrab++;
002: bcra zl $su, "<", $svyr
002: be arkg;
002:
002: cevags "%03q: %f", $svyrab, $_ juvyr <$su>;
002: }
002:
002: __RAQ__
makholm@makholm:/tmp$
//Makholm
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2968
***************************************