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.
Loading
VulpesPosted Sep 5, 2011, 11:51 AM
Michael MalcolmPosted Sep 6, 2011, 6:14 AM
VulpesPosted Sep 5, 2011, 6:56 PM
Michael MalcolmPosted Sep 5, 2011, 6:08 PM
So if I make the values -1 then I check to see if there is -1 and this would work the same as if array is empty then do.
My understanding of Array's isn't very strong. That is why I asked for help.
I thought that I could:
while (array is empty)
generate random number
if number isn't in the array
add it
Thanks for your time.