Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 chilling in the Monastery: (7)
As of 2024-03-28 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found