0

I started coding about a week ago in school and got an assignment to make a 20x20 box and fill it randomly with the characters B, V, M, SB, SL, _, F, P, K, L. I also have to code the chance that the character is in the box. For example: B has an 8% chance to be in my 20x20 box. I'm not sure how to get the characters to appear randomly and no clue at all how to code a certain chance for them all.

This is what I have so far:

int[,] objectArray = new int[20, 20];

// Horizontal numbers.
for (int i = 1; i <= 20; i++)
{
    if (i == 1)
        Console.Write("   " + i);
    else if (i < 9)
        Console.Write("  " + i);
    else
        Console.Write(" " + i);

}
Console.WriteLine("");

//Vertical numbers
for (int x = 0; x < 20; x++)
{
    if (x < 9)
        Console.Write((x + 1) + "  ");
    else
        Console.Write((x + 1) + " ");

    for (int y = 0; y < 20; y++)
    {
        Console.Write("_  ");
    }

    Console.WriteLine();
}

Console.Read();

I hope you can give me tips on how to fix this.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
Noomie
  • 29
  • 1
  • Welcome to StackOverflow! Fix what? What _exactly_ is not working and _exactly how_ should it behave instead? – René Vogt Feb 15 '16 at 17:34
  • The code is working for as far as compiling the 20x20 box goes, but I have no idea how to randomly put characters in there that have a certain chance of appearing :( – Noomie Feb 15 '16 at 17:41

1 Answers1

0

C# has plenty of built in Random Number Generator code that would help you out here - I would suggest reading this question about how to generate random numbers between certain values: Produce a random number in a range using C#

If you then place each of your chosen characters ( B, V, M, SB, SL, _, F, P, K, L. I) in an array, then you can reference them using the random numbers that have been generated.

Some code like the following will print out 20 random values from your list of string - so you can adapt this to do exactly what you need.

string[] chars = new string[] { "B", "V", "M", "SB", "SL", "_", "F", "P", "K", "L" };
Random rand = new Random();
for (int i = 0; i < 20; i++) {
      Console.WriteLine(chars[rand.Next(0, chars.Length - 1)]);
}
Community
  • 1
  • 1
geckojk
  • 178
  • 6