4
Answers

Random Array of numbers

Ask a question
Hi there, I would like to figure out how to shorten a piece of code.
I have it working the way I want it to work however I know it can be simplified, I just can't figure out how.

I want to make a do while loop that checks to see if the array is empty.

int draw;
            int[] balls = new int[5];
            int size = balls.Length;
            int count = 0;


            Random rdmn = new Random();
           
            for (int i = 0; i < size; i++)
            {
               
                draw = rdmn.Next(50);
                if (!balls.Contains(draw))
                {
                    balls[count] = draw;
                    count++;
                }
                else
                {
                    size++;
                }
            }


            foreach (int n in balls)
            {
                Console.WriteLine(n);
            }

Right now I am adding to the size of the loop if the number is not unique. However if I can put in a do while loop then I can get rid of int size, etc.


Thank for your time.

Answers (4)