I'll assume what you mean to ask is "How do I select 160 unique and random lines from a group of 1000 using perl."
If you are doing an actual scientific study, you'll probably need to familiarize yourself with perl's rand function more, to be able to argue that it is truly random.
First problem: unique lines. If you simply do rand(1000), you run the chance of getting duplicate values. Not a problem. For this, we use splice.
Second problem: select random lines. For this we use rand(), as you have surmised. Lets look it up at perldoc.perl.org and see what it says:
"Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. .... Automatically calls srand unless srand has already been called.
Apply int() to the value returned by rand() if you want random integers instead of random fractional numbers."
You don't need to use srand, as it is called automatically. Yay perl.
" splice ARRAY,OFFSET,LENGTH
Removes the elements designated by OFFSET and LENGTH from an array.... In scalar context, returns the last element removed, .... The array grows or shrinks as necessary."
This can be used to trim out values out of an array, much like using a scissor to cut away lines from a paper and save them in a scrapbook.
Next, for clarity, I remind you of the beauty of the pound sign, which perl considers as the start of a comment that is not evaluated by the perl script. You can add and remove lines this way, and add clarifying comments, such as mine below.
So here's the program:
#!/user/bin/perl -w
# Use your own path
use strict; # always use strict
my $file = "sample.txt";
open (FILE, $file) or die "$0: $!"; # printing errors is a Good Thing (tm)
my @file =
; # Store all the lines in an array
close FILE;
open OUT, ">my_samples.txt" or die "$0: $!"; # Caution: will overwrite any existing files
my $i;
for ($i = 0; $i<160;$i++) { # Start sampling loop
my $rand = int(rand($#file));
# rand gives you a random number between 0 and the number of lines remaining in the array, note
# that this number is by necessity dynamic, it will drop 1000, 999, 998, etc.
my $sample = splice @file, $rand, 1; # Cut out the $rand-th line, offset 1 means just one line
print OUT $sample;
} # end of for loop
Being a simple program, it should work right away, but you may need to tweak it. You can run it several times and compare the results by using ">>" instead of ">" in the open() statement. ">>" means append, ">" means truncate and start a new file (overwrite).
If you want to sample the randomness in a clever fashion, swap the file content in the array with numbers 0-999, print it to file, and analyze it in Excel, or your favourite statistic program. The simple way to make such an array is:
my @file = (0 .. 999);
Note that this will require you to comment out the other lines where we open the file, and read it, and put the file content into @file.
Good luck!