Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I have a dataset of around 1000 lines. Out of these 1000 lines I need to pick randomly 160 lines of data and write it to a file. The ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    4

    Randomly selecting 160 lines within a data set of 1000 in Perl

    Hello,

    I have a dataset of around 1000 lines. Out of these 1000 lines I need to pick randomly 160 lines of data and write it to a file. The program is needed to eliminate data bias when I run the program through a reanalysis program. I am thinking I need to use the rand or srand term, but I am having difficulty writing this in perl. I have to write it in perl, because the rest of my scripts for this project are in perl, so consistency is important. The data only consists of one column of the data (YYYYMMDDHHHH). Thanks in advance for the help.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Welcome to the forum.

    See perl module Sample.pm:
    Code:
    This package gives two methods to draw fair, random samples from a set.
    There is a procedural interface for the case the entire set is known,
    and an object oriented interface when the a set with unknown size has
    to be processed.
    If it is not in your repository, you can get it at: search.cpan.org/~abigail/Algorithm-Numerical-Sample-2010011201/

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Post

    It's quite simple as in C:

    perldoc -f rand

    rand EXPR
    rand Returns a random fractional number greater than or equal to 0
    and less than the value of EXPR. (EXPR should be positive.) If
    EXPR is omitted, the value 1 is used. Currently EXPR with the
    value 0 is also special-cased as 1 (this was undocumented before
    Perl 5.8.0 and is subject to change in future versions of Perl).
    Automatically calls "srand" unless "srand" has already been
    called. See also "srand".

    Apply "int()" to the value returned by "rand()" if you want
    random integers instead of random fractional numbers. For
    example,

    int(rand(10))

    returns a random integer between 0 and 9, inclusive.

    (Note: If your rand function consistently returns numbers that
    are too large or too small, then your version of Perl was
    probably compiled with the wrong number of RANDBITS.)
    So, that's the simplest solution:

    Code:
    #!/usr/bin/perl
    
    $count = 300;
    @indexes;
    
    # make a new pseudo-random sequence
    srand time;
    
    while($count--) {
        push @indexes, int(rand(1000));
    }
    @indexes will contain your random-generated indexes for strings.

Posting Permissions

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