4

假设我有 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)
4

3 回答 3

2

不知道为什么你会搞乱嵌套的 case 语句。

每次你需要生成一个新的随机数时,把你的权重加起来。

然后使用Random.Next(sumOfWeights).

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

那是你的选择。然后将该重量减少5。

于 2012-12-23T08:01:34.530 回答
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;
    }
}
于 2012-12-23T08:02:04.497 回答
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;
        }
    }
}
于 2012-12-23T08:48:53.690 回答