随机执行带有加权值的操作
本文关键字:操作 加权 执行 行带 随机 | 更新日期: 2023-09-27 17:49:31
假设我有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仍然有机会被随机选中但没有其他的那么多
示例输出:(控制台输出)
1 (weight = 95)
3 (weight = 95)
1 (weight = 90)
5 (weight = 95)
1 (weight = 85)
2 (weight = 95)
不知道为什么要在嵌套的case语句中瞎折腾。
每次你需要生成一个新的随机数时,把你的权重加起来。
则使用Random.Next(sumOfWeights)
。
然后将返回的随机数与第一个权值、前两个权值之和、前三个权值之和等进行比较,直到小于。
这是你的选择。
下面是一段简单的代码,如果我理解得好,你需要什么,可以用这个,作为起点:
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;
}
}
另一种方法:
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;
}
}
}