MLUG: Re: [MLUG] grab_random_lines
Re: [MLUG] grab_random_lines
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Mike Miller wrote:
> On Wed, 21 May 2008, Stephen Montgomery-Smith wrote:
> 
>> I would do it in perl, and read the whole file into memory.  Unless your 
>> file is absolutely huge (bigger than physical memory) I bet this will be 
>> much faster:
>>
>> #/usr/bin/perl
>>
>> open(IN,$ARGV[0]);
>> @lines=<IN>;
>> for ($i=0;$i<$ARGV[1];$i++) {
>>   print $lines[int(rand($#lines+1))];
>> }
> 
> 
> That's great!  My files actually are small, so this will surely be faster 
> than what I was doing.  The longest file right now is about 1100 lines.
> 
> Did you switch the order of the arguments?

I might have.  I wasn't paying attention.

> 
> Does int(rand(x+1)) produce a uniformly-distributed random integer between 
> 1 and x?

The perlfunc man page seems to suggest that this is so.  I guess that 
for bootstrapping that the quality of the random number generator is 
rather important.

> 
> Mike
> 
> _______________________________________________
> members mailing list
> EMAIL:PROTECTED
> http://mlug.missouri.edu/mailman/listinfo/members
> 
> 


_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members