Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,
I want to make a "quiz" program that randomly selects a line from a text file (QUESTIONS.txt) containing the quiz questions. I then need the randomly selected line to display in a text file.
I have attempted this alone and failed, then tried searching online but couldn't find anything helpful.

If someone could just give me the code that I require, I would be more than grateful!
If you could also label the code with simple comments just to let me know how it works I will be ecstatic!!

Thank you so much in advance!!
Posted
Updated 3-Jun-15 2:30am
v2

Let's ponder over the problem solving process...
1. Read the text file line by line as string values into a List<keyvaluepair><int,>> with randomized integer key
2. Sort this List by its keys.
3. Read this List into an Array.
4. Cycle through this Array to get the randomly sorted texts.
Next, no sweat no gain, get your hands dirty by mixing and matching these examples from the following links to work out your solution:
1. http://www.dotnetperls.com/streamreader[^]
2. http://www.dotnetperls.com/shuffle[^]
You may begin...
 
Share this answer
 
v2
Comments
Matt T Heffron 3-Jun-15 13:16pm    
Tactful, Helpful, Didn't give code!
+5
We're not here to do your work for you.
 
Share this answer
 
Comments
Member 11738700 3-Jun-15 22:32pm    
I hope you don't think that i am lazy, because I am not.
I just wanted someone to show me the code i should use and why I should use it.
C# is difficult for me...I'm much better at HTML and CSS
josephcj87 13-Aug-20 1:42am    
Just because someone asks a question doesn't make them lazy. Maybe they spent a lot of time doing research, and they still couldn't find the answer. At that point, it is okay to ask for help. What would do @F-ES Sitecore if you could not figure something out?
C#
//Here is something that may help you get started
//You can produce a shuffled List in 3 lines of code
        Random random = new Random();
        var lines=   File.ReadAllLines(@"C:\temp\test.txt");
        var shuffled= lines.OrderBy(line => random.Next());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900