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

Time:02-21

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])}");
}

  • this is what I currently have.

CodePudding user response:

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

  •  Tags:  
  • c#
  • Related