首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用百分比在几个操作之间进行选择

使用百分比在几个操作之间进行选择
EN

Stack Overflow用户
提问于 2018-04-20 16:00:44
回答 3查看 52关注 0票数 0

我有一个兴趣爱好数量为X的对象,每个爱好的值在0-100之间(代表%),它们加在一起达到100。

每天早上我都想运行一个函数来决定这个对象要做什么,一个爱好的价值越高,它们就越有可能被选中。

我不知道如何将最后一部分转换成代码。例如,使用这些变量:

代码语言:javascript
复制
    int fishing = 25;
    int sleeping = 25;
    int drinking = 50;
    int running = 0;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-04-20 16:19:20

这对你是有用的:

代码语言:javascript
复制
 class Program
{
    private static List<KeyValuePair<string, int>> Actions = new List<KeyValuePair<string, int>>()
    {
        new KeyValuePair<string, int>("fishing", 25),
        new KeyValuePair<string, int>("sleeping", 25),
        new KeyValuePair<string, int>("drinking", 5),
        new KeyValuePair<string, int>("running", 0),
    };

    static void Main(string[] args)
    {
        var result = Actions.OrderByDescending(r => r.Value).First();

        Console.WriteLine(result.Key);
    }
}

这假设您已经有了操作及其百分比。现在,如果任何2(或更多)项具有相同的百分比,它将显示第一个添加到列表中。

编辑

抱歉没有达到随机化的要求。试试这个:

代码语言:javascript
复制
var listOfActionsToTake = new List<string>() { "fishing", "sleeping", "drinking", "running" };

        var listOFActions = new List<KeyValuePair<string, int>>();

        var rand = new Random();
        var currentPercentage = 101;


        for (int i = 0; i < listOfActionsToTake.Count; i++)
        {
            var current = rand.Next(currentPercentage);
            if (i == listOfActionsToTake.Count - 1)
            {
                current = currentPercentage - 1;
            }
            listOFActions.Add(new KeyValuePair<string, int>(listOfActionsToTake[i], current));

            currentPercentage -= current;
        }
        foreach (var action in listOFActions)
        {
            Console.WriteLine($"{action.Key}: {action.Value}");
        }

        var result = listOFActions.OrderByDescending(r => r.Value).First();

        Console.WriteLine(result.Key);

像这样,无论您在listOfActionsToTake中添加了多少值,它们之间的总和总是为100,并且最大的值将被选中。

票数 0
EN

Stack Overflow用户

发布于 2018-04-20 16:45:57

我找到了的例子,这似乎是有意义的。您可以将这些值相加以获得累积值。如果所创建的随机值小于累积概率,则当前项被选中。

基本上,如果该条件为false,则忽略该条件中当前项的概率。因此,下一个要比较的项目有一个更高的概率被选择。

例如:

代码语言:javascript
复制
// 1. RandNum = 0.4
// 2. cumulative = 0.0
// 3. fishing = 0.25
// 4. cumulative = cumulative + fishing = 0.25
// 5. RandNum < cumulative ? false

随机数为0.4,渔业可能有0.25的选择。由于捕鱼的概率比随机数低,我们把它加到累积值中,在下一项中基本上忽略它。下一个项目将有更高的概率被选择,因为现在它只是sleepingdrinking。他们现在都有一个被选择的.50概率。

代码语言:javascript
复制
// 1. RandNum = 0.4
// 2. cumulative = 0.25
// 3. sleeping = 0.25
// 4. cumulative = cumulative + sleeping = .50
// 5. RanNum < cumulative ? true

虽然您必须修改它,以考虑到百分比是相等的,因为它只需要第一个。在找到要执行的间接操作后,您可以检查百分比是否相同。如果随机选择的动作与另一个动作的百分比相同,那么随机选择其中的一个动作。

代码语言:javascript
复制
static void Main(string[] args)
{
    int fishing = 25;
    int sleeping = 25;
    int drinking = 50;
    int running = 0;

    List<KeyValuePair<string, double>> elements = new List<KeyValuePair<string, double>>();
    elements.Add(new KeyValuePair<string, double>("fishing", (fishing * (1.0 / 100.0)))); // 25 * (1 /100) = .25
    elements.Add(new KeyValuePair<string, double>("sleeping", (sleeping * (1.0 / 100.0))));
    elements.Add(new KeyValuePair<string, double>("drinking", (drinking * (1.0 / 100.0))));
    elements.Add(new KeyValuePair<string, double>("running", (running * (1.0 / 100.0))));


    Random r = new Random();
    double rand = r.NextDouble();

    double cumulative = 0.0;
    string selectedElement = "";
    for (int i = 0; i < elements.Count; i++)
    {
        cumulative += elements[i].Value;
        if (rand < cumulative)
        {
            selectedElement = elements[i].Key;
            break;
        }
    }
    Console.WriteLine("Selected Element: " + selectedElement);
    Console.ReadKey();
}

// Test 1: rand: 0.522105917
//         cumulative: 1
//         Selected Element: drinking
// Test 2: rand: 0.49201479
//         cumulative: 0.5
//         Selected Element: sleeping
票数 0
EN

Stack Overflow用户

发布于 2018-04-20 16:48:14

这应该可以做到,并将工作时,他们不加100也。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static Dictionary<string, int> activities = new Dictionary<string, int>
    {
        { "running", 0 },
        { "fishing", 25 },
        { "sleeping", 25 },
        { "drinking", 50 }
    };

    static void Main(string[] args)
    {
        int sum = activities.Sum(a => a.Value);
        int rand = new Random().Next(sum);
        int total = -1;
        string activity = activities.SkipWhile(a => (total += a.Value) < rand).First().Key;
        Console.WriteLine(activity);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49945641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档