[Tutor] reading random line from a file

Alan Gauld alan.gauld at btinternet.com
Sun Jul 15 08:38:29 CEST 2007


"max baseman" <dos.fool at gmail.com> wrote


> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly

You can either get theclines to be the same length and
use a random index to seek() to the start of the line you want.
Or you can build a separate index file which records where
each line starts and randomly select one of thiose. But that
requires that the quotes file is only changed programmatically
so that the index file can be rebuilt each time you add/delete
a quote. (Or you build an indexing program)

Its much easier (unless the file is huge) to just use readlines()

HTH

Alan G.




More information about the Tutor mailing list