1

This code is buggy but can't figure out why ... want to populate an array with 7 unique random integers without using arraylists or linq! I know the logic is not okay...

class Program
{
    static void Main(string[] args)
    {    int current;
         int[] numbers = new int[7];  // size of that array 
         Random rNumber = new Random();
         current = rNumber.Next(1, 50);
         numbers[0] = current;
         Console.WriteLine("current number is {0}", current);
         for (int i=1;i<7;i++)
         {
             current = rNumber.Next(1, 50);
             for (int j = 0; j < numbers.Length; j++)
             {
                 do
                 {
                     if (current == numbers[j])
                     {
                         Console.WriteLine("Duplicate Found");
                         current = rNumber.Next(1, 50);
                     }
                     else
                     {   
                         numbers[j++] = current;
                         break;
                     }
                 }while (current == numbers[j]);

             }//inner for

         }//outer for
         for (int l = 0; l < 7; l++) // DISPLAY NUMBERS
         {
             Console.WriteLine(numbers[l]);
         }

    }// main
 }//class
1
  • 1
    Have you tried stepping through with a debugger? Commented Nov 11, 2011 at 20:09

4 Answers 4

4

want to populate an array with 7 unique integers without using arraylists or linq!

int[] list = new int[7];
for (int i = 0; i < list.Length; i++)
{
    list[i] = i;
}


EDIT

I changed your inner loop, if the random number is already in the array; create a new random and reset j to 0.

        for (int i = 1; i < 7; i++)
        {
            current = rNumber.Next(1, 50);
            for (int j = 0; j < numbers.Length; j++)
            {
                if (current == numbers[j])
                {
                    Console.WriteLine("Duplicate Found");
                    current = rNumber.Next(1, 50);
                    j = 0; // reset the index iterator
                }
            }//inner for
            numbers[i] = current; // Store the unique random integer
        }//outer for
Sign up to request clarification or add additional context in comments.

6 Comments

We all know that the OP meant 7 unique random integers, however this is certainly the best answer to the question that was actually asked. +1
I meant Random gentlemen my apologies
Downvoters, please explain your hate. Or explain where I'm wrong. Thanks
It is not generating random numbers between 1 and 50 and there is no garanties that there is no duplicates.
@Casperah, where is 1, 50 a requirement?
|
3

I presume you are looking for random numbers, so the other answer is not what you are looking for.

There are a couple of issues here.

  • The inner loop is testing for duplicates. However, it is looking from 0 through the end of the array since it is using numbers.length. This should probably be i, to compare with already set values. numbers.length is always 7 regardless of whether or not you set any of the elements.

  • the assignment is using j, so presuming the first element is not a duplicate, it will be overwritten each time. That should be numbers[i] = current;. No ++ necessary as the for is handling the incrementing.

  • if you determine that a number is a duplicate, j should be reset to zer to check against the entire list again rather than having the while in the middle.

Without a complete rewrite, the changes will look something like this:

     for (int i=1;i<7;i++)
     {
         current = rNumber.Next(1, 50);
         for (int j = 0; j < i; j++)  //----------------- loop through set values
         {
             if (current == numbers[j])
             {
                 Console.WriteLine("Duplicate Found");
                 current = rNumber.Next(1, 50);
                 j = 0; // -----------------------reset the counter to start over
             }
         }//inner for

         // if we got here there is no duplicate --------------------------------
         numbers[i] = current;

     }//outer for

(Please note that I have not tested this code, just added the changes)

1 Comment

tested it out i totally forgot about resetting j to 0 ,
1

you keep overwriting the same indexes in the else, and also checking too many indices causing the first to show up as a duplicate at all times which was false...

change it to:

 for (int i=1;i<7;i++)
 {
     current = rNumber.Next(1, 50);

     for (int j = 0; j < i; j++)    ///< change to j < i.  no need to check the others
     {
         do
         {
             if (current == numbers[j])
             {
                 Console.WriteLine("Duplicate Found");
                 current = rNumber.Next(1, 50);
             }
             else
             {   
                 numbers[i] = current;   ///< not j++ but i to prevent writing at the same locations over and over again
                 break;
             }
         }while (current == numbers[j]);
     }//inner for
 }//outer for

Comments

0

What about this?

int[] list = new int[7]; 
var rn = new Random(Environment.TickCount);
for (int i = 0; i < 7; i++) 
{ 
    var next = rn.Next(1, 50);
    while(Contains(list, next))
    {
        next = rn.Next(1, 50);
    }
    list[i] = next;       
} 


private bool Contains(IEnumerable<int> ints, int num) 
{
    foreach(var i in ints)
    {
        if(i = num) return true;
    }
    return false;
} 

3 Comments

shouldn't that be getting a new value while(Contains(next)) instead of !contains?
Adding the second function improves readability considerably.
Excellent and simple, but maybe use rNumber.Next(1, 50) instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.