2

Okay, the title might be kind of misleading, but I couldn't really come up with a better explanation for my problem. I'm trying to create a program that takes an array of first names and an array of last names, and puts these together in an array of full names. Problem here is that I'm trying to make sure that the same name doesn't occur twice, so that if a combination of firstname[randomindex] and lastname[randomindex] is already in the full names array, it will reroll the random number used for index untill it finds a combination that has not been used. So far I'm kinda stuck at this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _100randompersons
{
class Fyldefylde
{
    private string[] firstnames;
    private string[] lastnames;
    private string[] fullnames;
    private Random random;

    public Fyldefylde()
    {
        firstnames = new string[10] { "Kim", "Allan", "Frank", "Lone", "Line", "Anne", "Per", "Bo", "Grethe", "Mette" };
        lastnames = new string[10] { "Pedersen", "Nielsen", "Hansen", "Larsen", "Nygaard", "Harboe", "Bendix", "Højris", "Pilgaard", "Nyager" };
        fullnames = new string[100];
        random = new Random();
    }

    public string[] getFirstNames()
    {
        return firstnames;
    }

    public string[] getLastNames()
    {
        return lastnames;
    }

    public string[] getFullNames()
    {
        return fullnames;
    }

    public int getRandomIndex(int min, int max)
    {
        int randomnumber = random.Next(min, max);
        return randomnumber;
    }

    public void fillFullNames()
    {
        for(int i = 0; i < fullnames.Length; i++)
        {
            getRandomIndex(0, 10);
            while(fullnames.Contains(firstnames[getRandomIndex(0,10)] + " " + lastnames[getRandomIndex(0,10)]))
                {
                fullnames[i] = firstnames[getRandomIndex(0, 10)] + " " + lastnames[getRandomIndex(0, 10)];
                }
        }
    }

}
}

Thanks in advance! :)

sunero4
  • 820
  • 9
  • 29
  • 2
    why do it random? you could just fill the full names with a nested `for` iterating all combinations – prof1990 Jan 19 '17 at 11:02
  • Are you trying to generate a fixed number of random combinations or all possible combinations? In the former, random makes sense, in the latter it doesn't. – InBetween Jan 19 '17 at 11:03

3 Answers3

3

It looks like you just want to make all combinations of full names, so why not do that?

for (int i = 0; i < firstnames.Length; i++)
{
    for (int j = 0; j < lastnames.Length; j++)
    {
        fullnames[i*lastnames.Length + j] = firstnames[i] + " " + lastnames[j];
    }
}

Then if you want your names in a random order, run a method to randomize the order of names

If you want the names in a random order then you could always do something like the following

string[] firstnames = new string[10] { "Kim", "Allan", "Frank", "Lone", "Line", "Anne", "Per", "Bo", "Grethe", "Mette" };
string[] lastnames = new string[10] { "Pedersen", "Nielsen", "Hansen", "Larsen", "Nygaard", "Harboe", "Bendix", "Højris", "Pilgaard", "Nyager" };
string[] fullnames = new string[100];

List<int> indices = new List<int>();
for (int i = 0; i < firstnames.Length * lastnames.Length; i++)
{
    indices.Add(i);
}

Random random = new Random();
for (int i = 0; i < firstnames.Length; i++)
{
    for (int j = 0; j < lastnames.Length; j++)
    {
        int randomIndex = random.Next(indices.Count() - 1);
        fullnames[indices[randomIndex]] = firstnames[i] + " " + lastnames[j];
        indices.RemoveAt(randomIndex);
    }
}
Ben
  • 514
  • 2
  • 10
2

You could use a simple HashSet to make sure that values are unique:

    public void fillFullNames()
    {
        HashSet<string> newFullNames = new HashSet<string>();
        while (newFullNames.Count != fullnames.Length) {
            newFullNames.Add(firstnames[getRandomIndex(0, 10)] + " " + lastnames[getRandomIndex(0, 10)]);
        }
        fullnames = newFullNames.ToArray();
    }
  • Thanks, I'm pretty new to c# and programming in general, so I wasn't familiar with the Hashset command, but I'll look into it :)! – sunero4 Jan 19 '17 at 11:13
  • 1
    HashSet.Add(T value) only returns true (and adds the element) if the new value is not yet present in the HashSet. – Thomas Bienkowski Jan 19 '17 at 11:22
1

Your best bet is to generate a prototype version of fullname which represents every possible combination, then shuffle that if you want randomised output:

Random rnd = new Random();
string[] random_full_name = fullname.OrderBy(x => rnd.Next()).ToArray();  

You then read this new array out consecutively to achieve your random sample with no duplciates.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks for the help! I think after seeing Bens comment about just using a nested for loop I kinda made it harder for myself than I needed to, but now I know this is an option if I need to do something like this another time :) – sunero4 Jan 19 '17 at 11:06
  • This is short, but not the most efficient way to shuffle a list. Check this topic: http://stackoverflow.com/questions/273313/randomize-a-listt – Vilx- Jan 19 '17 at 11:08