Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. christmas exchange list generator

christmas exchange list generator

Scheduled Pinned Locked Moved C#
data-structurestutorialregexquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aei_totten
    wrote on last edited by
    #1

    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?

    B 1 Reply Last reply
    0
    • A aei_totten

      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?

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      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)

      A 1 Reply Last reply
      0
      • B Ben Fair

        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)

        A Offline
        A Offline
        aei_totten
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups