C# 将男孩和女孩随机分为c组

C# 将男孩和女孩随机分为c组,c#,list,math,random,C#,List,Math,Random,以前也有人问过类似的问题,但没有回答我的问题 我有3、4、5和6个小组,每个小组应该有50%的男孩和50%的女孩。用户确定每组的数量以及男孩和女孩的数量 例如:12个女孩,15个男孩,1*3,2*4,2*5和1*6组 我已经有一个随机的例子。我现在如何将随机选择的男孩/女孩平均分组 男孩和4人组随机分组: //for each group of 4 for (int i = 0; i < tischgruppenVierer; i++) { //only do this two t

以前也有人问过类似的问题,但没有回答我的问题

我有3、4、5和6个小组,每个小组应该有50%的男孩和50%的女孩。用户确定每组的数量以及男孩和女孩的数量

例如:12个女孩,15个男孩,1*3,2*4,2*5和1*6组

我已经有一个随机的例子。我现在如何将随机选择的男孩/女孩平均分组

男孩和4人组随机分组:

//for each group of 4
for (int i = 0; i < tischgruppenVierer; i++)
{
    //only do this two times because 50% boys
    for (int j = 0; j < 4/2; j++)
    {
        var picked = namesBoys[random.Next(0, namesBoys.Count())];
        namesBoys.Remove(picked); //no duplicates
        picked = new TischVier().student;
    }
}
我希望这对你来说足够了,因为我为每个小组都硬编码了。
我欣赏每一个想法,因为我几乎绝望。

更多信息,请参见代码中的注释:

//setup

//hold the group sizes we want to make - you say your user chose this
int[] groupSizes = new[] {3, 4, 4, 5, 5, 6};

//you have lists of people from somewhere
List<Person> boys = new List<Person>();
for(int i = 0; i < 15; i++)
  boys.Add(new Person());
List<Person> girls = new List<Person>();
for(int i = 0; i < 12; i++)
  girls.Add(new Person());

//logic of random grouping
List<List<Person>> groups = new List<List<Person>>();
Random r = new Random();

bool takeBoy = false;

//for each groupsize we make
foreach(int g in groupSizes){ 

  List<Person> group = new List<Person>(); //new group
  for(int i = 0; i < g; i++){ //take people, up to group size

    //take boys and girls alternately
    takeBoy = !takeBoy;
    var fr = takeBoy ? boys : girls;

    //if there are no more boys/girls, take other gender instead
    if(fr.Count == 0) 
      fr = takeBoy ? girls : boys;

    //choose a person at random, less than list length
    var ri = r.Next(fr.Count);

    //add to the current grouping
    group.Add(fr[ri]);

    //remove from consideration
    fr.RemoveAt(ri);
  }
  //group is made, store in the groups list
  groups.Add(group);
}
和演示小提琴:使用始终以男孩为先的逻辑


另一个演示小提琴:使用全局交替男孩/女孩学习逻辑

我将在课堂上总结这一点,课堂上包含两个男孩和女孩列表,以及获得指定大小组的方法。在伪代码中,此方法如下所示:

洗牌男孩名单和女孩名单 将布尔开关初始化为男孩和女孩之间的触发器 对于每个迭代,计数到所需的大小。。。。 根据布尔标志从列表中弹出一个男孩或女孩 翻转布尔值 在实际代码中,这类似于:

public IEnumerable<string> GetGroup(int size)
{
    Shuffle(boys);
    Shuffle(girls);
    if((boys.Count + girls.Count) < size)
    {
            throw new ArgumentException("Not enough people to satisfy group");
    }
    bool isBoy = rng.NextDouble() > 0.5;
    for(var i = 0;i<size;i++)
    {
        string next = "";
        if(isBoy)
        {
            yield return PopBoy();
        }
        else
        {
            yield return PopGirl();
        }
        isBoy = !isBoy;
    }
}

完整的工作代码可以在这里找到:

您的需求仍然需要澄清。尝试编辑您的问题并澄清您的需求。你的代码没有帮助。你怎么能用50%的男孩和50%的女孩来填充一组3人?@DStanley这是我的下一个问题。你能不能只是随机排列男孩和女孩的列表,然后以交替模式从每个列表中选择一个?顺便说一句,这也解决了上面提到的问题,我想奇数群体会有更多的性别和更多的成员?e、 g.如果男孩多于女孩,3人组将是b、b、g,5人组将是b、b、b、g、g?我认为拥有一个全局计数器或确保每次只在组内翻转会更好,因为这样会产生更均匀的分布。假设组大小为3,3,4。AFAIU代码将生成最后一组相同性别的2+1、2+1、0+4,而不是更好的2+1、1+2、2+2分布。
public IEnumerable<string> GetGroup(int size)
{
    Shuffle(boys);
    Shuffle(girls);
    if((boys.Count + girls.Count) < size)
    {
            throw new ArgumentException("Not enough people to satisfy group");
    }
    bool isBoy = rng.NextDouble() > 0.5;
    for(var i = 0;i<size;i++)
    {
        string next = "";
        if(isBoy)
        {
            yield return PopBoy();
        }
        else
        {
            yield return PopGirl();
        }
        isBoy = !isBoy;
    }
}
private string PopBoy()
{
    if(boys.Count>0)
    {
        var name = boys[0];
        boys.RemoveAt(0);
        return name;
    }
    else
    {
        return PopGirl();
    }
}
private string PopGirl()
{
    if(girls.Count>0)
    {
        var name = girls[0];
        girls.RemoveAt(0);
        return name;
    }
    else
    {
        return PopBoy();
    }
}