[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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
0
npl77
Asked:
npl77
  • 8
  • 7
1 Solution
 
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
 
npl77Author Commented:
what does the 1,6 mean?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now