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 ...
- 04-11-2011 #1Just 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.
- 04-12-2011 #2Linux Engineer
- 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:
If it is not in your repository, you can get it at: search.cpan.org/~abigail/Algorithm-Numerical-Sample-2010011201/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.
Best wishes ... cheers, drlWelcome - 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 )
- 04-12-2011 #3Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
It's quite simple as in C:
So, that's the simplest solution: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.)
@indexes will contain your random-generated indexes for strings.Code:#!/usr/bin/perl $count = 300; @indexes; # make a new pseudo-random sequence srand time; while($count--) { push @indexes, int(rand(1000)); }


Reply With Quote