* Michael Campbell (michael_s_campbell / yahoo.com) wrote: > > The way I do this is something like: > > > > File.open(aFile, 'r') do |f| > > tries = 0 > > begin > > # seek to a random point in the file > > f.seek(rand(File.size(aFile))) > > > > # read to the next tagline > This gives more "weight" to longer lines in the file though, no? In > other words, if I had a file of 2 quotes, one being 150k long, the > other about 25 characters, I'd rarely get the "short" one, since a > random seek would be in the middle of the long quote more often. Yes, this is what I said in my second message. In such a case, you'd build: 00001,000000 00002,153600 then seek to a random position in the index file. Since they're fixed sized records, you can mod the seek to hit on one record exactly, so there's no need to worry about discarding the first line and the obvious skewing you'll get from that on such a small file. > Not a show-stopper, certainly, and for all but the most contrived, > pathological cases (as I just made), this is an interesting solution I > hadn't considered. I use this algorithm on www.aagh.net to grab an entry out of a fortune file. Since they're all of a fairly similar length, and its not exactly a serious issue, I didn't bother with an index file :) I used it back in my ARexx days to generate my .signature too. Back then on a 25MHz CPU and a PIO-0 IDE drive, it was especially important not to end up scanning the entire file each time :) -- Thomas 'Freaky' Hurst - freaky / aagh.net - http://www.aagh.net/ - If you are a police dog, where's your badge? -- Question James Thurber used to drive his German Shepherd crazy.