3

I'm looking to create a 10x10 table in which the values are randomly generated from 1 to 20.

I have managed to create the table however I can't figure out how to make the value of a cell random, rather than a specific value.

How do I go about doing this?

4
  • 2
    Please post the code that you have to generate the table. Commented Jan 10, 2014 at 13:35
  • 2
    do you mean, you didn't know how to generate random number in c# ? Commented Jan 10, 2014 at 13:36
  • Have you considered using the System.Random class? msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx Commented Jan 10, 2014 at 13:37
  • I know how to create the random number, just not how to set it as the value for a cell Commented Jan 10, 2014 at 14:32

1 Answer 1

5

Simple, use Random class,

    Random r = new Random();
    r.Next(1, 20);

A simple 2-D array example,

    int[,] arr = new int [10, 10];
    Random r = new Random();
    for (int ki = 0; ki < 10; i++)
    {
        for (int kj = 0; kj < 10; j++)
        {
            arr[ki, kj] = r.Next(1, 20);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.