in reply to Random lines from a file for a quiz
Here's the code I have so far
As your code seems to overwrite your "answers.txt" file, I suspect you haven't done much testing....
In any case, reading the whole file into an array and then shuffling the array is one approach. Or, rather than shuffling, you could splice out a random line each iteration until there are no more lines left. If the file is so huge that you don't want to read the whole thing in (which is unlikely given the requirements) then, instead of reading the whole file into memory, you could scan it once and save offsets for each line in an array. Then you could shuffle your array of offsets and use seek() when it comes time to actually read the question in.
That should be enough to get you started. As this appears to be a learning exercise, giving you code would probably be counterproductive.
-sauoq "My two cents aren't worth a dime.";
|
|---|