C# 使用百分比在多个操作之间进行选择

C# 使用百分比在多个操作之间进行选择,c#,math,logic,C#,Math,Logic,我有一个有X个爱好的对象,每个爱好的值在0-100(代表%)之间,加起来就是100 每天早上我都想运行一个函数来决定这个对象要做什么,一个爱好的价值越高,他们被选中的可能性就越大 // 1. RandNum = 0.4 // 2. cumulative = 0.25 // 3. sleeping = 0.25 // 4. cumulative = cumulative + sleeping = .50 // 5. RanNum < cumulative ? true 我不知道如何将最后一

我有一个有X个爱好的对象,每个爱好的值在0-100(代表%)之间,加起来就是100

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

// 1. RandNum = 0.4
// 2. cumulative = 0.25
// 3. sleeping = 0.25
// 4. cumulative = cumulative + sleeping = .50
// 5. RanNum < cumulative ? true
我不知道如何将最后一部分翻译成代码。例如,使用这些变量:

    int fishing = 25;
    int sleeping = 25;
    int drinking = 50;
    int running = 0;
// 1. RandNum = 0.4
// 2. cumulative = 0.0
// 3. fishing = 0.25
// 4. cumulative = cumulative + fishing = 0.25
// 5. RandNum < cumulative ? false

这将对您有用:

 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);
    }
}
类程序
{
私有静态列表操作=新列表()
{
新的KeyValuePair(“钓鱼”,25),
新的KeyValuePair(“睡眠”,25),
新的KeyValuePair(“饮酒”,5),
新的KeyValuePair(“正在运行”,0),
};
静态void Main(字符串[]参数)
{
var result=Actions.OrderByDescending(r=>r.Value).First();
控制台写入线(结果键);
}
}
这假设您已经有了操作及其百分比。现在,如果任何2个(或更多)项目具有相同的百分比,它将显示添加到列表中的第一个项目

编辑

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

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);
var listOfActionsToTake=new List(){“钓鱼”、“睡觉”、“喝酒”、“跑步”};
var listOFActions=新列表();
var rand=new Random();
var currentPercentage=101;
for(int i=0;ir.Value).First();
控制台写入线(结果键);
这样,无论您向
ListofActionToTake
添加多少值,它们之间的总和始终为100,并且将选择最大的值

我找到了一个示例,这似乎是有意义的。添加这些值以获得累积值。如果创建的随机值小于累积概率,则当前项为选定项

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

例如:

    int fishing = 25;
    int sleeping = 25;
    int drinking = 50;
    int running = 0;
// 1. RandNum = 0.4
// 2. cumulative = 0.0
// 3. fishing = 0.25
// 4. cumulative = cumulative + fishing = 0.25
// 5. RandNum < cumulative ? false
尽管您必须修改它以考虑百分比相等的情况,因为它只取第一个。您可以在找到要执行的初始操作后检查百分比是否相同。如果随机选择的动作与另一个动作的百分比相同,则随机选择其中一个动作

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
static void Main(字符串[]args)
{
国际渔业=25;
int=25;
int=50;
int运行=0;
列表元素=新列表();
添加(新的KeyValuePair(“fishing”,(fishing*(1.0/100.0));//25*(1/100)=.25
添加(新的KeyValuePair(“休眠”,“休眠*(1.0/100.0)));
元素。添加(新的KeyValuePair(“饮酒”),(饮酒*(1.0/100.0));
添加(新的KeyValuePair(“running”,“running*(1.0/100.0)));
随机r=新随机();
double rand=r.NextDouble();
双倍累积=0.0;
字符串selectedElement=“”;
for(int i=0;i
这应该可以做到,并且在它们不增加到100的情况下也会起作用

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);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
班级计划
{
静态字典活动=新建字典
{
{“正在运行”,0},
{“钓鱼”,25},
{“睡觉”,25},
{“喝酒”,50}
};
静态void Main(字符串[]参数)
{
int sum=activities.sum(a=>a.Value);
int rand=new Random().Next(和);
int-total=-1;
字符串activity=activities.SkipWhile(a=>(total+=a.Value)
是的,但是说我将数字23随机化。然后呢?我唯一的办法就是让每个爱好都有一个范围。i、 e 0-24,25-50,51-100随机化在哪里进行?@Majs-对不起,没有得到这个要求。检查更新的答案