用加权值随机做动作

时间:2012-12-23 07:23:01

标签: c# random

假设我有5个结果

Console.WriteLine("1");
Console.WriteLine("2");
Console.WriteLine("3");
Console.WriteLine("4");
Console.WriteLine("5");

我想使用权重随机执行上述操作之一,因此我们可以说它们的权重从100开始。

它随机打印1并将其重量降低5,使其重量达到95.

所以在这之后,按升序排列的权重是(95,100,100,100,100),因此所有100个权重都有5%的机会随机选择超过95但95仍然有机会随机选择但没有其他人那么多。

示例输出:(控制台输出)

1 (weight = 95)
3 (weight = 95)
1 (weight = 90)
5 (weight = 95)
1 (weight = 85)
2 (weight = 95)

3 个答案:

答案 0 :(得分:2)

不知道你为什么要乱用嵌套的case语句。

每当您需要生成新的随机数时,请加权。

然后使用Random.Next(sumOfWeights)

然后将您返回的随机数与第一个权重,前两个权重的总和,前三个权重的总和等进行比较,直到它小于。

这是你的选择。然后减轻5的重量。

答案 1 :(得分:1)

这是一个简单的代码,如果我理解得很好,你需要什么,你可以使用它,作为一个起点:

class Program
{
    static void Main(string[] args)
    {
        List<ActionWithChance> list = new List<ActionWithChance>()
                                          {
                                              new ActionWithChance("1", 100),
                                              new ActionWithChance("2", 100),
                                              new ActionWithChance("3", 100),
                                              new ActionWithChance("4", 100),
                                              new ActionWithChance("5", 100)
                                          };

        for (int i = 0; i < 10; i++)
        {
            RandomHandler.CreateIntervals(list);
            RandomHandler.GetRandom(list);
        }


    }
}

static class RandomHandler
{
   public static void CreateIntervals(List<ActionWithChance> list)
    {
        int currentBorderMin = 1;
        int currentBorderMax = 0;
        foreach (var actionWithChance in list)
        {
            actionWithChance.TempMin = currentBorderMin;
            actionWithChance.TempMax = currentBorderMax 
                              + actionWithChance.Chance;

            currentBorderMax = actionWithChance.TempMax;
            currentBorderMin = currentBorderMax;
        }
    }

    public static void GetRandom(List<ActionWithChance> list)
    {
        Thread.Sleep(20);
        int allChance = list.Sum(i => i.Chance);
        Random rand = new Random();
        int nextValue = rand.Next(1, allChance + 1);
        ActionWithChance selectedAction = 
list.FirstOrDefault(i => i.TempMin <= nextValue && i.TempMax >= nextValue);

        selectedAction.Chance = selectedAction.Chance > 5 
            ? selectedAction.Chance - 5 : 100;

        selectedAction.DoSomething();
    }
}

class ActionWithChance
{
    public string Name { get; set; }
    public int Chance { get; set; }
    public int TempMin { get; set; }
    public int TempMax { get; set; }

    public void DoSomething()
    {
        Console.WriteLine(Name);
    }


    public ActionWithChance(string name, int chance)
    {
        Name = name;
        Chance = chance;
    }
}

答案 2 :(得分:0)

另一种方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace RandomWeights
{
    public class WeightedItem
    {
        public string Text { get; set; }
        public int Weight { get; set; }

        public WeightedItem(string text, int weight)
        {
            Text = text; 
            Weight = weight;
        }
    }
    public class WeightedOutput
    {
        public static readonly int _decreaseIncrement = 5;
        List<WeightedItem> items = new System.Collections.Generic.List<WeightedItem>();

        public WeightedOutput()
        {
            //initialize the five items with weight = 100
            for (int i = 1; i <= 5; i++)
                items.Add(new WeightedItem(i.ToString(), 100));

            for (int x = 0; x < 50; x++)
                WriteSelected();

            Console.ReadLine();
        }

        public void WriteSelected()
        {
            WeightedItem selectedItem = GetItem();
            if (selectedItem != null)
                Console.WriteLine(selectedItem.Text + ": " + selectedItem.Weight);
            else
                Console.WriteLine("All items have 0 probability of getting selected");
        }

        public WeightedItem GetItem()
        {
            int totalWeight = items.Sum(x=>x.Weight);
            Random rnd = new Random((int)DateTime.Now.Ticks);
            int random = rnd.Next(0, totalWeight);

            WeightedItem selected = null;
            foreach (var item in items)
            {
                if (random < item.Weight && item.Weight > 0)
                {
                    //need a new item and not a reference to get the right weights
                    selected = new WeightedItem(item.Text, item.Weight);
                    //decrease item's weight
                    item.Weight -= _decreaseIncrement;
                    break;
                }

                random -= item.Weight;
            }
            return selected;
        }
    }
}