npl77
asked on
Assigning a random spot in am array to another array C#
I have 2 arraylists with the following data
Array1
Sam
Matt
Jeff
and Array2
2
5
3
I want to assign Sam, Matt, and Jeff to the numbers in Array2 randomly
Array1
Sam
Matt
Jeff
and Array2
2
5
3
I want to assign Sam, Matt, and Jeff to the numbers in Array2 randomly
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what does the 1,6 mean?
ASKER
rnd.Next(1, 6) ??? Can you comment what that means
Random number should be between 1 and 6. min and max value. If you check intellisense, it shows parameters description.
Random.Next( minval, maxVal) generates a random number in the range minval to maxval. Minval is inclusive, maxval is exclusive. So the range of numbers it produces is minval to (maxval - 1). I run it in a loop because the numbers can repeat.
Jim
Jim
ASKER
index is never declared. Do you mean i instead of index? Also should the max number in rnd always be 1 more than the size of the arraylist?
Index is declared - at the top of the method just below the boolean array. This code runs.
Jim
Jim
Yes, the maxval is one larger than the number it will return. You could use:
(used.Length + 1) for maxval. In fact, if you pass the two array lists into the method already populated, it is best to use names.Length to set the size of the used array and the maxval (adding one).
Jim
(used.Length + 1) for maxval. In fact, if you pass the two array lists into the method already populated, it is best to use names.Length to set the size of the used array and the maxval (adding one).
Jim
ASKER
Having a problem using your code. If you do this:
for (int i = 0; i < 5; i++)
used[i] = false;
then you will never be able to enter the while loop since used[index-1] will never be true.
for (int i = 0; i < 5; i++)
used[i] = false;
then you will never be able to enter the while loop since used[index-1] will never be true.
I do not understand. I stepped through the code to verify that it worked as expected and it did. I'm not sure where you are having trouble with a loop?
Jim
Jim
ASKER
p is an arraylist with 2 names in it for some reason it never enters the while loop but in your code it does. The reason i think it never hits was because this isnt true until you get into the loop while (used[index - 1] == true)
bool[] used = new bool[p.Count];
int index = 0;
for (int i = 0; i < p.Count; i++)
used[i] = false;
Random rnd = new Random(unchecked((int)Date
for (int j = 0; j < p.Count; j++)
{
index = rnd.Next(1, p.Count + 1);
while (used[index - 1] == true)
{
index = rnd.Next(1, p.Count + 1);
used[index - 1] = true;
a[j] = p[index - 1];
}
}
ASKER
When debugging your code it works fine but when debugging my code it never gets into the loop here:
while (used[index - 1] == true)
{
//NEVER ENTERS HERE
index = rnd.Next(1, p.Count + 1);
used[index - 1] = true;
a[j] = p[index - 1];
}
while (used[index - 1] == true)
{
//NEVER ENTERS HERE
index = rnd.Next(1, p.Count + 1);
used[index - 1] = true;
a[j] = p[index - 1];
}
ASKER
dammit! i know why I put the wrong stuff in the loop only this index = rnd.Next(1, p.Count + 1); goes in the loop. Sorry, im just a beginner please forgive and Thanks
I see the problem - there's too much code in the while loop. Change it to this:
while (used[index - 1] == true)
{
index = rnd.Next(1, p.Count + 1);
}
used[index - 1] = true;
a[j] = p[index - 1];
I usually do not use braces to contain loop content when it's only executing one line. It would have been clearer if I had included them.
Jim
while (used[index - 1] == true)
{
index = rnd.Next(1, p.Count + 1);
}
used[index - 1] = true;
a[j] = p[index - 1];
I usually do not use braces to contain loop content when it's only executing one line. It would have been clearer if I had included them.
Jim
No worries. Good luck.
Jim
Jim
ASKER
Sam 5
Jeff 2
Matt 3
Just show the output as a writeLine or something