Hello all,

This one's a bit of a cheaky one....and tricky.

So i've managed to read a text file - that's brilliant.

Now, I wish to select a random word from there where the text file looks like this:

Blue
Red
Purple
Pink

etc.

All I want to do is randomly select one of the words. I may update the text file whenever I wish to and it should cause no affect to the program ( no added code for that, i know )

Any suggestions?

Try this:

private void method()
        {
            Random r = new Random();
            string[] colors = { "blue", "red", "purple", "pink" };
            string color = colors[r.Next(colors.Length)];
        }

Hi Mitja.

That randomly selects colours from an array, not from the file with the colours.

Random r = new Random();

private string Method() {
    String[] myString = File.ReadAllLines("myfilename.txt");
    return myString[r.Next(myString.Length)];
}

Never put the new Random in a method that might be called repeatedly, you won't like the results. Put it outside the method, or in the constructor.

If you are trying for 'random access' in the file, text files aren't good for that. You have no idea how many entries there are, so you'd have to read the entire file once just to count them, then read again to get the one you want.

Edited 5 Years Ago by Momerath: n/a

I've got this so far - Got some weird error:

namespace System
{
    public partial class Main : Form
    {
        string word;
        Random r = new Random();

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
           System.IO.StreamReader reader = new System.IO.StreamReader("C:\\janus.inf.brad.ac.uk\\malomran\\SOIProfile\\Desktop\\Colours.txt", Encoding.Default);
           String[] myString = File.ReadAllLines(ToString());
           word = myString[r.Next(myString.Length)];
           reader.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

       
    }
}

I also got this even stranger error: Error 1 Friend access was granted to 'System, PublicKey=00000000000000000400000000000000', but the output assembly is named 'System, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Try adding a reference to 'System, PublicKey=00000000000000000400000000000000' or changing the output assembly name to match. c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll System


Make sense out of that and i'll give you a chocolate bar!

Subby~

You are doing it wrong :) What it is reporting is the signature for the method ToString(), which you are trying to open in line 16. Put the name of the file there, i.e.

private void Main_Load(object sender, EventArgs e) {
    String[] myString = File.ReadAllLines("C:\\janus.inf.brad.ac.uk\\malomran\\SOIProfile\\Desktop\\Colours.txt");
    word = myString[r.Next(myString.Length)];
}

Edited 5 Years Ago by Momerath: n/a

I'm going to check that and see if it works.

Now, where would you like me to post your well earned chocolate bar?

I did what you said, made some changes and it still doesn't work.

This is now my coding:

private void Main_Load(object sender, EventArgs e)
        {
           StreamReader reader = new StreamReader("C:\\Users\\malomran\\Colours.txt");
           String[] myString = File.ReadAllLines(reader.ToString());
           word = myString[r.Next(myString[0].Length)];
           reader.Close();
        }

Help grately appreciated!

No, no, no, no.

It's not word = myString[r.Next(myString[0].Length)]; It's word = myString[r.Next(myString.Length)];

This article has been dead for over six months. Start a new discussion instead.