[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Needing a random number generator for scripting



* Anthony J. Breeds-Taurima (tony@cantech.net.au) [010926 23:14]:
> <== randline ==>
> #!/usr/bin/perl
> ## print 1 randomline from a file
> @_=<>;
> print $_[rand$#_];

while this one works, the one Martin posted is a better solution.
They're both O(n) in terms of time, but Martin's is O(1) wrt space,
whereas this one is Also O(n) -- it reads in and stores the entire file
and then picks out a line. Unfortunately, it doesn't lend itself as well
to extension to the case of "shuffling a file". But for one random line
from a file, it's a very elegant solution. See the archives for more
discussion.

-- 
Vineet                                   http://www.anti-dmca.org
Unauthorized use of this .sig may constitute violation of US law.
echo Qba\'g gernq ba zr\!             |tr 'a-zA-Z' 'n-za-mN-ZA-M'

Attachment: pgpkc0SF0MDpy.pgp
Description: PGP signature


Reply to: