-
Read specific line
Hello,
I'm looking at writing a password generator that uses real words. In order to do this I've downloaded a relatively small dictionary text file, about 80K words (600kb), with a word-per-line.
Now, my problem is what is the best way to pick a word out of this file randomly, preferably with FSO?
Currently I count the number of lines in the file and use a random number generator to pick a number within the lines-of-file range. This is where I run into problems/issues. I can read each line into an array, and access the random element as required, but is this the best/most efficient way for such a large number of lines?
Thanks.
-
Where did you got this dictionary? I want it too.:rolleyes:
-
There are a few at http://www.puzzlers.org/secure/wordlists/dictinfo.html
There will probably be loads available if you search the internet.
Now, back to my question, I can do it using the array method outlined above with FSO and the normal VB open file for input as #1 method. FSO is much slower, but I wanted to use it in case I wanted to use it in ASP in the future. But I suppose there's no problem with making it a dll.
But is that the best way to do it?
-
Why do you want to count how many lines it is every time? Dont you know that?
Then read Plenderjs tutorial about files (recommended)
http://209.120.143.185/showthread.ph...specific+lines
-
I do know the number of lines, but I can't guarantee new words won't be entered in the future.
I'll check out the link...
-
Unfortunately that didn't help. My existing method isn't unusably slow, but it is sluggish. Since I'm only returning a single word it seems like a long way to do it.
There must be a better way to do this...
Cheers.
-
Ok, sorry to keep banging on about this, but speed is important. Anyway, my new method is to use ADO to open the text file as a recordset, which can then be counted and accessed directly. I'll settle for this at the moment, but if anyone has any suggestions or knows a better way, please let me know.
Thanks.
-
Since the text file is big, even counting the lines would be a waste of time. An you have to count the lines if you want to randomly jump to a line in your file.
So I have a suggestion :
Each time the file is updated (added/removed words), count the lines/words and put the result in the first line of the file. For example, a dictionary file would look like this :
Code:
17
abac
abaca
abacate
abacay
abacinate
abacination
abaciscus
abacist
aback
abactinal
abactinally
abaction
abactor
abaculus
abacus
Abadite
abaff
since there are 17 words in the file.
Doing so you'll have 2 advantages :
1) Whenever you need a random word all you have to do is read the first line (which gives you the number of words), then generate a random number less or equal with the number of words, and jump to that word and retrieve it.
2) You'd only need to count again (and to update the first line) when your dictionary is updated. And that doesn't happen very often I suppose.
Cheers
-
Cheers, that's a good idea, but it's the jumping to specific line that I don't know how to do...
-
well then, import it into any indexed database format you like (say Access). You could make you own text-to-mdb import routine. Then you'd have an indexed dictionary that could be randomly accessed.
Let me know if I could help you with that.