-1

I'm having problems creating a random letter generator. Can anyone point me in the right direction?

I'm getting the error

Cannot implicitly convert string to int.

class Program
{
    static void Main(string[] args)
    {
        string[,] Grid = new string[5,5];

        string[] randomLetter = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        for (int i = 0; i < Grid.GetLength(0); i++)
        {
            for (int j = 0; j < Grid.GetLength(1); j++)
            {
                Random rng = new Random();
                int nextRandom = rng.Next(0, 26;
                string actualRandomLetter = randomLetter[nextRandom];
                Grid[i, j] = Grid[actualRandomLetter,actualRandomLetter];
            }
        }
    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Joseph
  • 120
  • 9
  • Which line is generating the error? – Yaman Dec 05 '16 at 23:15
  • 1
    Your code does not compile (missing `)` in `rng.Next(0, 26;`), so it's not clear what is causing the error - but the error should be clear. You cannot _implicitly_ convert a `string` to an `int`. You are passing strings to the array indexer (`Grid[actualRandomLetter,actualRandomLetter]`). I suspect you just want `Grid[i, j] = actualRandomLetter` – D Stanley Dec 05 '16 at 23:16
  • 2
    `Grid[actualRandomLetter,actualRandomLetter];`: This requires integers for the index, such as `Grid[0,3]` to retrieve the value located at index `(0,3)`, but you are passing in e.g., `Grid["A", "A"]` which doesn't make sense. Also do not construct a `new Random()` more than once, only make one then call `rng.Next` multiple times; see [here](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number/768001#768001) for why. – Quantic Dec 05 '16 at 23:18
  • 2
    BTW, do not create new `Random` for each random number. Use one `Random` for whole process instead. – user4003407 Dec 05 '16 at 23:21
  • Thanks. I've moved the 'Random'. Still stuck on populating the array though. – Joseph Dec 05 '16 at 23:31

2 Answers2

1

ActualRandomLeter is a string and you can't access the location (i.e. myArray["Hello"]) of an element in an array using a string. If you're trying to populate the array with the random letter you generated, this will do the trick:

public static void Main(string[] args)
    {
        string[,] Grid = new string[5, 5];
        string[] randomLetter = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        Random rng = new Random();

        for (int i = 0; i < Grid.GetLength(0); i++)
        {
            for (int j = 0; j < Grid.GetLength(1); j++)
            {                 
                int nextRandom = rng.Next(0, 26);

                string actualRandomLetter = randomLetter[nextRandom];

                Grid[i, j] = actualRandomLetter;

            }
        }
    }
Ryan Intravia
  • 420
  • 6
  • 12
0

Not sure if you want a 5x5 grid of 1-character strings, or an array of 5 strings of 5 characters each. There's not much difference between these, as both will allow you to do grid[i][j] to get the jth character in the ith row.

Here's an example that works:

// We'll output an array with 5 elements, each a 5-character string.
var gridSize = 5; 
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rand = new Random();
var grid = Enumerable.Range(0, gridSize)
    .Select(c=>new String(
        Enumerable.Range(0, gridSize)
        .Select(d=>alphabet[rand.Next(0, alphabet.Length)])
        .ToArray()
    )).ToArray();
Diego
  • 18,035
  • 5
  • 62
  • 66