Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: improving the efficiency of a script (random sample)

by ambrus (Abbot)
on Jun 18, 2006 at 20:46 UTC ( [id://556138]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    use warnings; use strict; my $g = 100; my @c; my $n = 0; while(<>) { i
    +f (rand() < $g / ++$n) { splice @c, int(rand(@c)), $g <= @c, $_; } } 
    +print for @c;
    
  2. or download this
    use warnings; use strict; my $g = 100; my %c; my %n; while(<>) { my $l
    + = /(.)/ && lc($1); my $c = \@{$c{$l}}; if (rand() < $g / ++$n{$l}) {
    + splice @$c, int(rand(@$c)), $g <= @$c, $_; } } print @$_ for values(
    +%c);
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://556138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-06-18 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.