gnucolx
gnucolx

Reputation: 1

What should I change so each line would have a different random amount of numbers?

I have written code where all lines have an equal amount of numbers. What should I change so each line would have a different random amount of numbers?

Random ran = new Random();
int x = ran.Next(2, 8);
int j = ran.Next(4, 10);

int[][] arr = new int[x][];
for (int i = 0; i < x; i++) 
{
    int[] sub = new int[j];
    for (int y = 0; y < j; y++) 
    {
      sub[y] = ran.Next(1, 10);
    }
    arr[i] = sub;
    Console.WriteLine($"{string.Join(",",arr[i])}");
}

Upvotes: 0

Views: 43

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59303

Move the declaration and assignment to j into the loop so you'll get a new random number each time instead of only once.

Upvotes: 1

Related Questions