I have a text file which actually is a book and I have to store 1000 random words to a string array. I was thinking to store first all the words of the text into an array and then create another array and randomly store 1000 words. Can I store imidiately the words randomly, I mean into the first array without use the second?
Here's one way if it's a text file with one word per line:
- Make one pass through the file, counting the words. Call this N.
- Select 1000 numbers from 1 to N.
- Sort the selected 1000 numbers.
- Make another pass through the file and save the words corresponding to the saved numbers.
@dhayden, for step 2, "select 1000 numbers from 1 to N", you want that without duplicates, so you should use a set. And using an (ordered) set also automatically sorts them. Then you can go through the dictionary file and efficiently pick up the words corresponding to the values in the set. Then you could finally sort that word list if desired.