CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2008
    Posts
    3

    Any idea how to randomize this?

    My Goal is for this data to be randomized every time it is read into the array from a file. Any suggestions?


    Code:
            public void Random_Load(object sender, EventArgs e)
            {
                System.IO.StreamReader wordsIn;
          string theWord;
          wordCount = 0;
           wordArray = new Word[100];
          try
          {
            wordsIn = new System.IO.StreamReader("wordData.txt");
            theWord = wordsIn.ReadLine();
            while (theWord != "EOF")
            {
              
              
              wordArray[wordCount++] = new Word(theWord);
              theWord = wordsIn.ReadLine();
              
            }
            wordsIn.Close();
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error processing input file\n" + ex.Message);
          }
    Last edited by NCLSEA123; November 14th, 2008 at 12:12 AM.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Any idea how to randomize this?

    I don't get it.

    What should be randomized?
    Which data?

    Data before writing or after reading from file?

    What kind of "random" you are looking for?

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: Any idea how to randomize this?

    Do you want the words from wordData.txt to be read and stored randomly in an array ?
    Please make clear your requirement.

  4. #4
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: Any idea how to randomize this?

    May be this is what your are looking for

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                StreamReader textReader;
    
                try 
                {
                    textReader = new System.IO.StreamReader(@"C:\\Words.txt");
                    string strTemp = textReader.ReadToEnd();
                    textReader.Close();
    
                    string[] words = Regex.Split(strTemp, @"[\s]");
                    Randomize(words); // here you get the array with random items.
                }
                catch (Exception ex)
                {
                     MessageBox.Show("Error processing input file\n" + ex.Message);
                }
            }
    
            static Random rng = new Random();
            static void Randomize(string[] list)
            {
                for (int i = list.Length - 1; i > 0; i--)
                {
                    int swapIndex = rng.Next(i);
                    if (swapIndex != i)
                    {
                        object tmp = list[swapIndex];
                        list[swapIndex] = list[i];
                        list[i] = tmp.ToString();
                    }
                }
            }
    Last edited by MMH; November 14th, 2008 at 04:05 AM.
    Regards,
    MMH
    Rate my post if you find it usefull.

  5. #5
    Join Date
    Nov 2008
    Posts
    3

    Re: Any idea how to randomize this?

    O.K., so basically, one of my program's goals is to read strings from a specfied textfile randomly, so one random word will appear in a text box during each run. Ultimately, I'm making a hangman style game that will show dashes in the textbox until the user enters the correct letters to make the word appear. The only real change I made to the code was I made the array global.
    Code:
     public partial class frm1 : Form
        {
            public static string[] wordArray;

    Code:
    public void frm1_Load(object sender, EventArgs e)
            {
                StreamReader wordsIn;
                try
                {
                    wordsIn = new System.IO.StreamReader("myData.txt");
                    string strTemp = wordsIn.ReadToEnd();
                    wordsIn.Close();
    
                    wordArray = Regex.Split(strTemp, @"[\s]");
                    Randomize(wordArray); // here you get the array with random items.
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error processing input file\n" + ex.Message);
                }
    
                int tester;
                string teststring;
    
                for (int i = 0; i > 0; i++)
                {
                    teststring = wordArray[i];
                    txtWord.Text = teststring;
                }
                for (int i = 0; i > 0; i++)
                {
                    tester = wordArray[i].Length;
                    txtLength.Text = tester.ToString();
                }
            }
    I assume that if this were working properly, the first value of the randomized array would be displayed in the text box, and the length of the string in the other textbox. (I need the length for later when I figure out how many dashes to draw) What dumb thing have I done?

  6. #6
    Join Date
    Nov 2008
    Posts
    15

    Re: Any idea how to randomize this?

    just read the array normally and randomize which one you choose to be the word chosen?
    Code:
    string[] items = items;
    
    Random rnd = new Random();
    rnd.Next(0, items.Length);
    
    string ChosenWord = items[rnd];
    and if you dont want the same word pulled twice just make an array of the numbers already pulled and iterate through that and make sure the rnd dosent equal?

    might have misunderstood something, but good luck!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured