如何在C#中均匀分享随机数

时间:2022-12-17 19:40:40

I am looking at sharing out a fixed number of 32 teams between a varied number of people.

我期待在不同数量的人之间分享固定数量的32支球队。

Of course, 32 may not always be evenly divisible, but for the sake of this exercise, lets say I am looking to share the 32 teams between 4 people, so a maximum number of 8 teams per person.

当然,32可能并不总是可以被整除,但是为了这个练习,让我说我想分享4个人之间的32支球队,所以每人最多可以有8支球队。

int max = 32 / numb;

foreach (string value in wcteams)
{
    //Assigning teams to players
    int selection = random.Next(0, numb);

    int[] counter = new int[max];
    counter[selection] = counter[selection] + 1;

    if (counter[selection] < max)
    {
        Console.WriteLine(inputtedNames[selection] + " has drawn " + value);
    }                    
}

Right now, I can run that code and I will get a list back of randomly chosen people along with their team. But the limit will not be implemented and some players will end up with more teams than others.

现在,我可以运行该代码,我将获得随机选择的人员及其团队的列表。但是限制将不会实施,一些球员最终将拥有比其他球队更多的球队。

I understand that the following code:

我理解以下代码:

counter[selection] = counter[selection] + 1;

Is not working to add up the number of teams that the user has received, am I on the right track here with how to tally up the number of times a player has been randomly selected or is there another method that I should be doing?

是不是在努力增加用户收到的团队数量,我是否在正确的轨道上如何计算一个玩家被随机选择的次数,还是我应该采取另一种方法?

2 个解决方案

#1


1  

One problem in your code is you are initializing counter inside the loop. Also what happens if the count[selection] > max? you leave the team and don't assign it to anyone else.

您的代码中的一个问题是您在循环内初始化计数器。如果计数[选择]>最大值会发生什么?你离开团队,不要把它分配给其他人。

Try the following code.

请尝试以下代码。

int numb = 4;
int max = 32 / numb;
int[] counter = new int[max];
foreach (string value in wcteams)
{
    bool selectionComplete = false;
    while(!selectionComplete)
    {
        int selection = random.Next(0, numb);
        counter[selection] = counter[selection] + 1;

        if (counter[selection] <= max)
        {
            selectionComplete = true;
            Console.WriteLine(selection + " has drawn " + value);
        }  
    }                      
}

#2


0  

I cannot figure your code but this should work.

我无法计算你的代码,但这应该工作。

public static Random randomT = new Random();
public static List<List<string>> DivideTeams(string[] teams, int personCount)
{
    List<List<string>> divideTeams = new List<List<string>>();
    if (teams.Length % personCount != 0)
    {
        throw new ArgumentOutOfRangeException();
    }
    //shuffle teams 
    for(int k = teams.Length -1; k > 0; k--)
    {
        int trade = random.Next(k + 1);
        string temp = teams[trade];
        teams[trade] = teams[k];
        teams[k] = temp;
    }
    for (int j = 0; j < personCount; j++)
    {
        divideTeams.Add(new List<string>());
        for (int i = 0; i < teams.Length / personCount; i++)
        {
            divideTeams[j].Add(teams[i]);
        }
    }           
    return divideTeams;
}

#1


1  

One problem in your code is you are initializing counter inside the loop. Also what happens if the count[selection] > max? you leave the team and don't assign it to anyone else.

您的代码中的一个问题是您在循环内初始化计数器。如果计数[选择]>最大值会发生什么?你离开团队,不要把它分配给其他人。

Try the following code.

请尝试以下代码。

int numb = 4;
int max = 32 / numb;
int[] counter = new int[max];
foreach (string value in wcteams)
{
    bool selectionComplete = false;
    while(!selectionComplete)
    {
        int selection = random.Next(0, numb);
        counter[selection] = counter[selection] + 1;

        if (counter[selection] <= max)
        {
            selectionComplete = true;
            Console.WriteLine(selection + " has drawn " + value);
        }  
    }                      
}

#2


0  

I cannot figure your code but this should work.

我无法计算你的代码,但这应该工作。

public static Random randomT = new Random();
public static List<List<string>> DivideTeams(string[] teams, int personCount)
{
    List<List<string>> divideTeams = new List<List<string>>();
    if (teams.Length % personCount != 0)
    {
        throw new ArgumentOutOfRangeException();
    }
    //shuffle teams 
    for(int k = teams.Length -1; k > 0; k--)
    {
        int trade = random.Next(k + 1);
        string temp = teams[trade];
        teams[trade] = teams[k];
        teams[k] = temp;
    }
    for (int j = 0; j < personCount; j++)
    {
        divideTeams.Add(new List<string>());
        for (int i = 0; i < teams.Length / personCount; i++)
        {
            divideTeams[j].Add(teams[i]);
        }
    }           
    return divideTeams;
}