0

I am trying to create 3 separate arrays with 7 random numbers. The problem I am running into is that when the 3 arrays are generated they all have the same set of 7 random numbers. None of them have different numbers. I am unsure on what I need to do in order for the 3 arrays to have different random numbers.

Here is the code I have so far.

static void Main(string[] args)
{
    int[] array1 = GenerateRandomArrayNumbers();
    int[] array2 = GenerateRandomArrayNumbers();
    int[] array3 = GenerateRandomArrayNumbers();

    DisplayArray(array1);
    DisplayArray(array2);
    DisplayArray(array3);
}

static void DisplayArray(int[] arr)
{
   foreach(int i in arr)
    {
        Console.Write($" {i} ");
    }
    Console.WriteLine();
}

static int[] GenerateRandomArrayNumbers()
{
    int[] RandonNumSet = new int[7];

    Random rNumber = new Random();

    for (int index = 0; index < RandonNumSet.Length; index++)
    {
        int generateNum = rNumber.Next(1, 50);
        RandonNumSet[index] = generateNum;
    }
    Array.Sort(RandonNumSet);
    return RandonNumSet;
}

Here is what it displays:

13  16  22  29  34  37  42
13  16  22  29  34  37  42
13  16  22  29  34  37  42

Thanks for any suggestions!

GSerg
  • 76,472
  • 17
  • 159
  • 346
Cole.C
  • 45
  • 5

1 Answers1

2

All of your new Random() instances share the same seed (since they're created at the same clock time).

You should either reuse a single instance or specify different seeds.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964