-4

I am looking to create a small windows app that will take a text file of hundreds, maybe thousands of lines of text and then randomize the text and print 5 lines seperated by a line break. I should be able to copy from the app and each time I hit the "generate" button it should delete the previous 5 text outputs.

Here's an example:

https://www.random.org/lists/

The difference is that this app randomizes and prints all lines. Could someone point me to some resources on how to do this exact thing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Blake King
  • 67
  • 1
  • 1
  • 7
  • Well, to help you start out, you need to go over the entire text file, search for line breaks, then you can make an array of Boolean with the size of the line breaks, initiated all to false. Lets say you have a file with 1000 lines, then you create array of 1000 boolean. Then you randomize a number 5 times, lets say you got 100,101,102,103,104 and 105. YourBoolArray[100] = true, ... You print those lines, next time, you keep random numbers that their array value is not true. good luck. – Ziv Weissman Feb 15 '15 at 08:57
  • Thank you. Could you tell me the function to use? I'm sure .net has something that does this already. – Blake King Feb 15 '15 at 09:13
  • Function to use for what? You have Random class - http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number For searching line break in file - http://stackoverflow.com/questions/6818094/deleting-line-breaks-in-a-text-file-with-a-condition – Ziv Weissman Feb 15 '15 at 09:21
  • here you should try first then search in google and if you have a technical issue in your code, post your code and we will help you. but if you don't try you don't learn –  Feb 15 '15 at 09:25

2 Answers2

3
  • Microsoft own c# developer portal has examples of how to read from a text file How to: Read From a Text File (C# Programming Guide) which can get you started with loading the text.
  • Microsoft's own Developer Network also has information on random number generation and examples at Random Class
  • finally Microsoft's own ASP.Net ASP.NET Samples has load of examples and information about building web (or desktop) applications.

You should be able to find working examples and API information on all three of these locations, that will help you with your quest of development of C# applications.

Code Uniquely
  • 6,356
  • 4
  • 30
  • 40
2
 //Initialize variables
    static Random rnd;
    static StreamReader reader;
    static List<string> list;

  //here we load the text file into a stream to read each line
        using (reader = new StreamReader("TextFile1.txt"))
        {
            string line;
            list = new List<string>();
            rnd = new Random();
            int index;

            //read each line of the text file
            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                //add the line to the list<string>
                list.Add(line);
            }

            //pull 5 lines at random and print to the console window
            for (int i = 0; i < 5; i++)
            {
                index = rnd.Next(0, list.Count);
                Console.WriteLine(list[index]);
            }
        }
        Console.ReadKey();
SuncoastOwner
  • 263
  • 2
  • 9