Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据其百分比从arraylist中选择值_Java_C#_Arraylist - Fatal编程技术网

Java 根据其百分比从arraylist中选择值

Java 根据其百分比从arraylist中选择值,java,c#,arraylist,Java,C#,Arraylist,我有一个物品的数组列表。每个对象有2个值,内容和重量。内容只是一个简单的字符串,而权重决定了字符串的使用频率百分比 如果阵列中有2个对象: 目标1: 内容:你好 体重:20 目标2: 内容:嘿 体重:80 第一个对象内容应使用20%的时间,而第二个对象内容应使用80%的时间 我如何根据权重决定选择哪些内容 到目前为止,我获得了以下代码: foreach (ContentModels cont in match_array) { if (match_array.Count == 1)

我有一个物品的数组列表。每个对象有2个值,内容和重量。内容只是一个简单的字符串,而权重决定了字符串的使用频率百分比

如果阵列中有2个对象:

目标1: 内容:你好 体重:20

目标2: 内容:嘿 体重:80

第一个对象内容应使用20%的时间,而第二个对象内容应使用80%的时间

我如何根据权重决定选择哪些内容

到目前为止,我获得了以下代码:

foreach (ContentModels cont in match_array)
{
     if (match_array.Count == 1)
     {
         match = cont.content;
         break;
     } 
 }

如果arraylist只包含一个对象,但如果有多个对象,我不知道该怎么办?好的,我想尝试一下,而不看其他人的代码。这是我想到的

顺便说一句,我希望是Java标记出错,而不是C标记:D

这是整个节目。下面是对每件作品的解释

我选择将每个元素作为比率中的一部分。因此,在您的示例中,您的总数是100 20+80,这意味着20内容模型应在20%的时间内被选择。如果要约束内容模型,使其总权重加起来等于100,则应在创建内容模型时进行约束

这是我的解决方案。 首先是内容模型:

然后是测试用例列表:

考虑到这些测试用例,我们希望看到“Hello”在20/80+90+20*100的时间内出现大约10.5%。对于其余的测试用例,依此类推

以下是实现这一点的生成器:

这里我们要做的就是计算出我们要处理的总重量。然后,我们将选取一个随机数,检查每个模型,询问这个数字是否来自这个内容模型?如果否,则减去该内容模型的权重并移动到下一个,直到我们得到一个选择权重小于0的模型。在这种情况下,我们选择了模型。我希望这是有道理的

注意:我选择每次重新计算总重量,以防您更改选项的源列表。如果将该列表设置为只读,则可以将该.Sum调用移到while循环之外

static IEnumerable<string> GetGreetings()
{
    Random generator = new Random();

    while (true)
    {
        int totalWeight = contentOptions.Sum(x => x.Weight);
        int selection = generator.Next(0, totalWeight);
        foreach (ContentModel model in contentOptions)
        {
            if (selection - model.Weight > 0)
                selection -= model.Weight;
            else
            {
                yield return model.Content;
                break;
            }
        }
     }
}
最后,这里有一个主要的方法也将测试整个过程:

以下是我的演练结果:


Java标记有错误吗?你想得到对象的最大或最小百分比是多少?如此详细的帖子,非常感谢!非常感谢,这是一个非常好的答案。我要更改的唯一一件小事是将int totalWeight=contentOptions.Sumx=>x.Weight;在while循环之外,因为该值永远不会改变,也不必重新计算。我使用字典将您的方法调整为通用扩展方法…并给了您应得的分数:
static List<ContentModel> contentOptions = new List<ContentModel>
{
    new ContentModel
    {
        Content = "hello",
        Weight = 20
    },
    new ContentModel
    {
        Content = "hey",
        Weight = 80
    },
    new ContentModel
    {
        Content = "yo dawg",
        Weight = 90
    }
};
static IEnumerable<string> GetGreetings()
{
    Random generator = new Random();

    while (true)
    {
        int totalWeight = contentOptions.Sum(x => x.Weight);
        int selection = generator.Next(0, totalWeight);
        foreach (ContentModel model in contentOptions)
        {
            if (selection - model.Weight > 0)
                selection -= model.Weight;
            else
            {
                yield return model.Content;
                break;
            }
        }
     }
}
static void Main(string[] args)
{
    List<string> selectedGreetings = new List<string>();

    /* This will get 1000 greetings, 
     * which are the Content property of the models, group them by the greeting,
     * count them, and then print the count along with the greeting to the Console.
     */
    GetGreetings()
        .Take(1000)
        .GroupBy(x => x)
        .Select(x => new { Count = x.Count(), Content = x.Key })
        .ToList()
        .ForEach(x => Console.WriteLine("{0} : {1}", x.Content, x.Count));

    Console.ReadLine();
}