christmas exchange list generator
-
Here is what I am trying to do. I have treeview that is full of a family tree. Example parent1 -spouce1 -kid1 -secondkid1 parent2 -kid2 parent3 -spouce3 -kid3 parent4 (but I don't really know how many parents and kids there are. So, now what I want to do is randomly match up each person with someone else, but not someone in thier own node. For example, I don't want parent1 to get matched up with his spouce or kids, but he would be fine matched up with parent2 or his kid. My brain is about mush right now and I am not thinking of how to do this, though there must be a simple way. My original idea was to put everything into arrays. Then choose the longest array and randomly chhose another array then randomly choose a member of that array to match up with the first person, then remove both people from the arrays and calculate the longest array again and repeat until there is no one left. But implementing the idea seemed more complicated and I thought there must be an easier way, no?
-
Here is what I am trying to do. I have treeview that is full of a family tree. Example parent1 -spouce1 -kid1 -secondkid1 parent2 -kid2 parent3 -spouce3 -kid3 parent4 (but I don't really know how many parents and kids there are. So, now what I want to do is randomly match up each person with someone else, but not someone in thier own node. For example, I don't want parent1 to get matched up with his spouce or kids, but he would be fine matched up with parent2 or his kid. My brain is about mush right now and I am not thinking of how to do this, though there must be a simple way. My original idea was to put everything into arrays. Then choose the longest array and randomly chhose another array then randomly choose a member of that array to match up with the first person, then remove both people from the arrays and calculate the longest array again and repeat until there is no one left. But implementing the idea seemed more complicated and I thought there must be an easier way, no?
If you want it to be more random you could use your original idea but with a random number generator to determine which array to look at rather than just selecting the longest array. Another idea would be to put everything in a single array and just use the random number generator to give a random index into the array and if the item in the index is in a different family, you've got a hit otherwise get another index from the random number generator.
List<int> peopleUsed = new List<int>();
Random r = new Random();
int person1Index, person2Index;
person1Index = person2Index = 0;
while(peopleUsed.Count < people.Length) // people is the array of people
{
if(!peopleUsed.Contains(person1Index)) // make sure person1Index hasn't been used
{
// find a person2Index that hasn't been used
do
{
person2Index = r.Next(0, people.Length);
}
while(peopleUsed.Contains(person2Index) && !peopleAreInSameFamily(person1Index, person2Index)); // also make sure they aren't in the same family!
// DO SOMETHING WITH THE 2 PEOPLE
peopleUsed.AddRange(new int[] { person1Index, person2Index }); // add the two person indexes to our used list
}
person1Index++;
}Keep It Simple Stupid! (KISS)
-
If you want it to be more random you could use your original idea but with a random number generator to determine which array to look at rather than just selecting the longest array. Another idea would be to put everything in a single array and just use the random number generator to give a random index into the array and if the item in the index is in a different family, you've got a hit otherwise get another index from the random number generator.
List<int> peopleUsed = new List<int>();
Random r = new Random();
int person1Index, person2Index;
person1Index = person2Index = 0;
while(peopleUsed.Count < people.Length) // people is the array of people
{
if(!peopleUsed.Contains(person1Index)) // make sure person1Index hasn't been used
{
// find a person2Index that hasn't been used
do
{
person2Index = r.Next(0, people.Length);
}
while(peopleUsed.Contains(person2Index) && !peopleAreInSameFamily(person1Index, person2Index)); // also make sure they aren't in the same family!
// DO SOMETHING WITH THE 2 PEOPLE
peopleUsed.AddRange(new int[] { person1Index, person2Index }); // add the two person indexes to our used list
}
person1Index++;
}Keep It Simple Stupid! (KISS)
the problem I worry about with that is if you don't start with the longest list, then you might end up at the end with only members of one family