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
npl77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

npl77Author Commented:
the end output should be for example

Sam 5
Jeff 2
Matt 3

Just show the output as a writeLine or something
0
JimBrandleyCommented:
Here's one approach.

Jim

public void Test()
{
   StringBuilder sb = new StringBuilder(256);
   ArrayList names = new ArrayList(5);
   ArrayList numbers = new ArrayList(5);
   bool[] used = new bool[5];
   int index = 0;
 
   for (int i = 0; i < 5; i++)
      used[i] = false;
 
   names.Add("Sam");
   names.Add("Matt");
   names.Add("Jeff");
   names.Add("Tom");
   names.Add("Dick");
 
   numbers.Add(2);
   numbers.Add(5);
   numbers.Add(3);
   numbers.Add(49);
   numbers.Add(22);
 
   Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
   for (int j = 0; j < 5; j++)
   {
      index = rnd.Next(1, 6);
      while (used[index - 1] == true)
         index = rnd.Next(1, 6);
      used[index - 1] = true;
      sb.Append((string)names[j] + " " + numbers[index - 1].ToString() + "\n");
   }
   string result = sb.ToString();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
npl77Author Commented:
what does the 1,6 mean?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

npl77Author Commented:
rnd.Next(1, 6) ??? Can you comment what that means
0
monarch_ilhanCommented:
Random number should be between 1 and 6. min and max value. If you check intellisense, it shows parameters description.
0
JimBrandleyCommented:
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
0
npl77Author Commented:
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?
0
JimBrandleyCommented:
Index is declared - at the top of the method just below the boolean array. This code runs.

Jim
0
JimBrandleyCommented:
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

0
npl77Author Commented:
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.
0
JimBrandleyCommented:
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
0
npl77Author Commented:
   
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)DateTime.Now.Ticks));
                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];
                    }
                 
                }
0
npl77Author Commented:
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];
                    }
0
npl77Author Commented:
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
0
JimBrandleyCommented:
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
0
JimBrandleyCommented:
No worries. Good luck.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.