3

I'm trying to create an array list using random numbers. But sometimes I get a zero in results. I do not understand why.

I'm grateful if anyone can explain.

int[] number = new int[6];
Random rnd = new Random();
for (int i = 0; i < number.Length; i++)
{
   int random = rnd.Next(1, 26);
   if (!number.Contains(random))
   {
     number[i] = random;
   }
}
foreach (int nr in number)
{
  Console.Write("|" + nr + "|");
}
//results
|6||12||0||22||25||11|
Örjan
  • 39
  • 2
  • 4
    Take out `if (!number.Contains(random))` and you will see how the 0 got there. _Hint - if the same random number is generated twice._ – mjwills Dec 24 '20 at 12:43
  • 1
    A better solution here would be to use a `HashSet` rather than an array. Call `Add` in a loop until the `Count` == 6. Then call `ToArray`. Duplicates will be automatically catered for. – mjwills Dec 24 '20 at 12:45
  • 1
    https://stackoverflow.com/questions/15300073/what-is-the-default-value-of-a-member-in-an-array and https://stackoverflow.com/questions/26931528/random-number-generator-with-no-duplicates are the most likely appropriate duplicates. – mjwills Dec 24 '20 at 12:47
  • 1
    add else statement with 'i--', to regenerate value in case of duplication – Karim Moghnieh Dec 24 '20 at 12:49

4 Answers4

3
int[] number = new int[6];

Here number array is created with default int value i.e 0

The issue with your code is in some cases this value is not getting updated due to the check
if (!number.Contains(random))

You can change your code to include a loop to guarantee your random number doesn't lie in the array.

int[] number = new int[6];
Random rnd = new Random();
for (int i = 0; i < number.Length; i++)
{
   int random = rnd.Next(1, 26);
   while (number.Contains(random))
   {
      random = rnd.Next(1, 26);
   }
   number[i] = random;
}
foreach (int nr in number)
{
  Console.Write("|" + nr + "|");
}

Please note that current approach is quite performance hungry as for every new random value we are iterating through entire array everytime to check if it exists. You can reduce the performance by using as HashSet<int> if possible

Jasmeet
  • 1,315
  • 11
  • 23
  • Another problem is that if `number = new int[24]` (or in general its length is near the range of the random numbers) it could become difficult to "find" non-already-used numbers. – xanatos Dec 24 '20 at 13:30
0
  1. When you declare the array in following statement it initializes with 6 integers as 0 int[] number = new int[6];
  2. While the random number generated for each array element, check for duplication in following statement may have resulted false if (!number.Contains(random))

That's why it was never updated to new assigned number. You can add else condition to it and regenerate random number

Tech-leo
  • 62
  • 5
0

Just use your debugger to step through your code and inspect your variables to see what's happening. Apart from all the suggestions here, which are bad because they can loop way too many times or even forever, you appear to want to get 6 random, unique numbers between 1 and 26.

The de facto way to do that, is to generate a list with those numbers (Enumerable.Range()), shuffle them (Fisher-Yates) and Take() the first six.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
-1

Use while loop.

int[] number = new int[6];
Random rnd = new Random();
int i = 0;
while (i < number.Length)
{
   int random = rnd.Next(1, 26);
   if (!number.Contains(random))
   {
     number[i] = random;
     i++;
   }
}
foreach (int nr in number)
{
  Console.Write("|" + nr + "|");
}