Originally posted by Darin Niard:
110,000 strings isn't that much.
Originally posted by Ernest Friedman-Hill:
At a 100 characters or so each, two bytes per character, plus say a dozen bytes of overhead per object, that's about 23 megabytes. Funny how the definition of "not that much" has changed!
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Originally posted by Darin Niard:
Interesting, but... if the first fortune has a 50% probability then why would it show up any less than 1/2 the time? Or, am I not understanding you correctly?
As for the index files, even if it picks the 5000th entry, does it not have to run through the first 4999 in order to find it? This is something about file i/o that's never been clear to me...
Originally posted by Ernest Friedman-Hill:
You always read the whole file, and you perform this procedure for every fortune. The first fortune thus has up to N-1 chances to be replaced -- if it's not replaced by the second one, it may be replaced by the third one, or fourth one, and so on. And once the first one is replaced, the replacement can itself be replaced, as can that replacement. So the probability of the first fortune being selected is 1/2 - 1/3 - 1/4 - 1/5 - 1/6 ... - 1/N, which is equal to, as it turns out, 1/N.
Originally posted by Darin Niard:
Clever, but I think the way I've always picked a random line from a file can be slightly more efficient. Basically, I'd just count up to a random number between 0 and the fileLength and stop on that entry.
Originally posted by Ernest Friedman-Hill:
[QB]
You always read the whole file, and you perform this procedure for every fortune. The first fortune thus has up to N-1 chances to be replaced -- if it's not replaced by the second one, it may be replaced by the third one, or fourth one, and so on. And once the first one is replaced, the replacement can itself be replaced, as can that replacement. So the probability of the first fortune being selected is 1/2 - 1/3 - 1/4 - 1/5 - 1/6 ... - 1/N, which is equal to, as it turns out, 1/N.QB]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |