0

I'm writing a simple language translator which will present the English word and ask for the corresponding foreign word. So far the code will select a random word from an array, however i'm now trying to get it to select the corresponding foreign word from the list. Here's the code so far:

public class Words
    {
        public int spanishindex; 
        string[] EnglishWords = { "Yellow", "Yello", "Yelow", "Yllow", "ellow" };
        string[] SpanishWords= { "giallo", "giall", "iallo", "gllo", "lo" };

        public String GetRandomWord()
        {
            Random randomizer = new Random();
            index = randomizer.Next(EnglishWords.Length);
            string randomword = EnglishWords[randomizer.Next(index)];
            spanishindex= index;
            return randomword;
        }

        public String MatchSpanishWord()
        {
            string matchword = SpanishWords[spanishindex];
            return matchword;
        }
    }

My thoughts were by passing in the index value oppose to a random value in the MatchSpanishWord method i would get the corresponding word (as the list is in order)

So if 'ellow' is selected the spanish equivalent should be 'lo'

Any help would be appreciated, thanks.

Hulaz
  • 257
  • 1
  • 3
  • 13
  • 3
    As a side note `Random randomizer = new Random();` should be outside of the function or else you will be asking a question [like these](http://stackoverflow.com/search?q=random+[C%23]+same+numbers) in a few hours when you have more code done. – Scott Chamberlain Mar 20 '13 at 19:53

3 Answers3

3

The problem is that you were calling random twice: Once to generate the random index, and then again in the array index. I fixed your bug in the code below:

public class Words
{
    public int spanishindex; 
    string[] EnglishWords = { "Yellow", "Yello", "Yelow", "Yllow", "ellow" };
    string[] SpanishWords= { "giallo", "giall", "iallo", "gllo", "lo" };

    public String GetRandomWord()
    {
        Random randomizer = new Random();
        index = randomizer.Next(EnglishWords.Length);
        string randomword = EnglishWords[index]; //<---- this is the fix
        spanishindex= index;
        return randomword;
    }

    public String MatchSpanishWord()
    {
        string matchword = SpanishWords[spanishindex];
        return matchword;
    }
}
w.brian
  • 16,296
  • 14
  • 69
  • 118
  • 1
    Another bug to note: instantiating `Random` before each use may lead to numbers that are [less "random" than you'd like](http://stackoverflow.com/q/767999/238688), especially if `GetRandomWord` is called inside a loop - better to make `randomizer` a class field with a static initializer. – Dan J Mar 20 '13 at 19:56
0

Change:

string randomword = EnglishWords[randomizer.Next(index)];

to

string randomword = EnglishWords[index];

... assuming you don't want a random index less than index you already randomly found.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
0

You should change

  string randomword = EnglishWords[randomizer.Next(index)];

to

  string randomword = EnglishWords[index];
Sumi
  • 127
  • 2
  • 10