in reply to Re^2: Opening random files then reading random lines from file.
in thread Opening random files then reading random lines from file.

I read elsewhere (sorry but I forgot where) that there's one potential technical problem with this algorithm:

If your rand(n) function isn't strictly uniform, neither will your random line selection, and this problem tends to get worse if the total number of lines increases.

A naively implemented rand(n) is often non-uniform because your RNG outputs integers between 0 and RMAX, where RMAX is usually a power of 2, or a large prime. The quick-n-dirty way to turn this into a random number between 0 and n, is to calculate its remainder modulo n. This is only truly uniform if n exactly divides RMAX, otherwise it will be almost uniform (given that RMAX is big), which is good enough for many purposes, but not if you're doing many calls and the correctness of your algorithm depends on the uniformity of your rand(n). The proper way to do it, btw, is to take the modulo in most cases, but re-roll if the RNG output is larger than n * floor(RMAX / n).

I am not sure how the rand(n) function in Perl works internally, hopefully it's been corrected over the years, but for other languages it's something to be on the lookout for. The only one I'm certain of is that Python's got the correct algo, because I remember reading a bug report about their random range function.

  • Comment on Re^3: Opening random files then reading random lines from file.

Replies are listed 'Best First'.
Re^4: Opening random files then reading random lines from file.
by jdporter (Paladin) on Sep 11, 2012 at 17:26 UTC