pick N random lines from a file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default pick N random lines from a file

    I'm trying to extend the Perl cookbook recipe on how to pick a random
    line from a file:

    #!/usr/bin/perl
    rand($.) < 1 && ($line = $_) while <>;
    print $line;

    for picking up to N random lines from a file:

    ------------start code--------------
    #!/usr/bin/perl

    die "Usage: $0 <N>, where N is the number of lines to pick\n"
    if @ARGV < 1;
    $N = shift @ARGV;

    @pick = ();
    while (<>) {
    if (@pick < $N) {
    push @pick, $_;
    ($r1, $r2) = (rand(@pick), rand(@pick));
    ($pick[$r1], $pick[$r2]) = ($pick[$r2], $pick[$r1]);
    } else {
    rand($.) <= $N and $pick[rand(@pick)] = $_;
    }
    }

    print @pick;
    -------------end code---------------

    Could anyone verify if the algorithm is correct?

    Thanks in advance,
    --
    dave


    David Garamond Guest

  2. Similar Questions and Discussions

    1. Random lines in DW tables with Flash text?
      I have designed some pages in DW MX 2004, and I find random lines like large hand-drawn dashes scattered about some of the table cells. The tables...
    2. How to pick a specific row in a file?
      Hello.. Lets say I want to pick row number #3 in text.txt. $row = file; How to do now..? :-P Thanks alot in Advance.
    3. Random increase in space between last 2 lines of paragraph!
      I have been puzzled for some time by apparently random occurrences of a rather strange and irritating problem. Documents I regularly create in...
    4. read lines of file without parsing the lines
      Hello! Currently i have a logfile which tracks a certain feature on my server. Every time the feature accurs my script appends a line in the...
    5. random pick from list
      If I have a list, how can I randomly pick one item from it when I click on a button? Thanks Shane
  3. #2

    Default Re: pick N random lines from a file

    On Wed, 2003-12-10 at 11:12, David Garamond wrote:
    > I'm trying to extend the Perl cookbook recipe on how to pick a random
    > line from a file:
    >
    > #!/usr/bin/perl
    > rand($.) < 1 && ($line = $_) while <>;
    > print $line;
    >
    > for picking up to N random lines from a file:
    >
    > ------------start code--------------
    > #!/usr/bin/perl
    >
    > die "Usage: $0 <N>, where N is the number of lines to pick\n"
    > if @ARGV < 1;
    > $N = shift @ARGV;
    >
    > @pick = ();
    > while (<>) {
    > if (@pick < $N) {
    > push @pick, $_;
    > ($r1, $r2) = (rand(@pick), rand(@pick));
    > ($pick[$r1], $pick[$r2]) = ($pick[$r2], $pick[$r1]);
    > } else {
    > rand($.) <= $N and $pick[rand(@pick)] = $_;
    > }
    > }
    >
    > print @pick;
    > -------------end code---------------
    >
    > Could anyone verify if the algorithm is correct?
    Dave,

    Yes, your algorithm seems to work. I just made a file with a number
    (1-20) on each line, then ran it and it seems to work just fine.

    HTH,
    Kevin
    --
    Kevin Old <kold@kold.homelinux.com>

    Kevin Old Guest

  4. #3

    Default Re: pick N random lines from a file

    Kevin Old writes:
    > On Wed, 2003-12-10 at 11:12, David Garamond wrote:
    > > I'm trying to extend the Perl cookbook recipe on how to pick a random
    > > line from a file:
    > >
    > > #!/usr/bin/perl
    > > rand($.) < 1 && ($line = $_) while <>;
    > > print $line;
    > >
    > > for picking up to N random lines from a file:
    The classical algorithm for this is found in Knuth's "Art of computer
    cproagramming, vol 2, seminumerical algorithms" on pp 121-123. Here
    is a C++ routine I wrote several years ago that implements the
    algorithm given in Knuth; perhaps it will help you write one in C:

    9: // Knuth's random sample algotithm S, "Seminumerical Algorithms" pp 121-123.
    10: //
    11: void sample(prng rand, long* slot, long n, long N) { // take a random sample
    12: long t; // counts up to N
    13: long m = 0; // counts up to n

    15: for (t = 0; m < n; t++) { // loop till all n slots are chosen
    16: if (rand.next(0, N - t) < n - m) { // do we select this from 0..N?
    17: slot[m++] = t; // yes,
    18: }
    19: }
    20: }

    --
    -------- "And there came a writing to him from Elijah" [2Ch 21:12] --------
    R. J. Brown III [email]rj@elilabs.com[/email] [url]http://www.elilabs.com/~rj[/url] voice 847 543-4060
    Elijah Laboratories Inc. 457 Signal Lane, Grayslake IL 60030 fax 847 543-4061
    ----- M o d e l i n g t h e M e t h o d s o f t h e M i n d ------
    Robert Brown Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139