Fethi Tekyaygil
Fethi Tekyaygil

Reputation: 333

C# Random number doesn't Generate 0

I'm working on a football league fixture project on C# Console Application. I'm trying to choose random teams from the array which contains the teams which plays at their home and away. When I'm trying to generate 9 random numbers, only 8 numbers are generated and 0 are not, so the code can't break the for loop. I suppose that the problem is that the if statement does not allow to generate the same number and int array's elements' default value is 0.

Here is the code and the output:

C# Code Output

    int randomHome; //Random number genetator for choosing a random iteration value from home array which containss the teams which plays at their home
    int randomAway; //Random number genetator for choosing a random iteration value from awayarray which containss the teams which plays at away

      Random randomNum = new Random();

      int[] randomHomeNumArray = new int[home.Length]; //array will hold the randomHome values and home array is the array which is holding the team's iteration values which plays at their home
      int[] randomAwayNumArray = new int[away.Length]; //array will hold the randomAway values and away array is the array which is holding the team's iteration values which plays at away
         for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
        {
            randomHome = randomNum.Next(home.Length)

            if (!randomHomeNumArray.Contains(randomHome) )
            {
                randomHomeNumArray[homeArrayCounter] = randomHome;   //It will hold the randomHome values
                Console.WriteLine(homeArrayCounter + ". iterasyon in Home " + randomHomeNumArray[homeArrayCounter]);
            }
            else
            {
                homeArrayCounter--;
            }
        }
        Console.WriteLine("\n\n");


        for (int awayArrayCounter = 0; awayArrayCounter < randomAwayNumArray.Length; awayArrayCounter++)
        {
            randomAway = randomNum.Next(randomAwayNumArray.Length);    

            if (!randomAwayNumArray.Contains(randomAway))
            {
                randomAwayNumArray[awayArrayCounter] = randomAway;     //It holds the random valures from away array which contains the teams which plays at away
                Console.WriteLine(awayArrayCounter + ". iterasyon in Away " + randomAwayNumArray[awayArrayCounter]);
            }

            else
            {
                awayArrayCounter--;
            }
        }

Upvotes: 3

Views: 1413

Answers (4)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

Because int is not a null-able data type, by default, an int[] gets initialized with zeroes. So even if you think it's an empty array, it's actually an array with all elements set to zero.

To rectify the problem, you can consider using an int?[] (null-able int array) instead. Or, you can initialize the array with either a negative integer or some integer greater than the maximum inclusive upper bound. Better yet, to achieve what you want, in a better way, use the solution provided by @Enigmativity, and mark his answer accepted, if it helped.

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117055

It appears you're trying to just randomize arrays.

Try this instead:

Random randomNum = new Random();

int[] randomHomeNumArray = Enumerable.Range(0, home.Length).OrderBy(_ => randomNum.Next()).ToArray();
int[] randomAwayNumArray = Enumerable.Range(0, away.Length).OrderBy(_ => randomNum.Next()).ToArray();

That's it. Done.

Upvotes: 2

Abhi N
Abhi N

Reputation: 968

When you initalize an array, it has the value 0 by default for each index. When you are using the random number, it always skips 0 because it already exists.

You can try like this:-

for(int i= 0; i<randomHomeNumArray.Length; i++){
            randomHomeNumArray[i] = -1;
        }

        for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
        {
            do{
                randomHome = randomNum.Next(home.Length);
            } while(!randomHomeNumArray.Contains(randomHome));
            randomHomeNumArray[homeArrayCounter] = randomHome;   //It will hold the randomHome values
            Console.WriteLine(homeArrayCounter + ". iterasyon in Home " + randomHomeNumArray[homeArrayCounter]);
        }

Upvotes: 3

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43886

Your problem is the default initialization of your arrays:

int[] randomHomeNumArray = new int[home.Length]; 

This creates an array filled with 0s, because 0 is the default value for int.

So your if condition

if (!randomHomeNumArray.Contains(randomHome))

is always false for 0 because 0 is already contained in the array.


You may initialize your arrays instead like this:

int[] randomHomeNumArray = Enumerable.Repeat(-1, home.Length).ToArray(); 

So you fill it with -1 instead of 0 and your if condition works.

Upvotes: 1

Related Questions