C# 广告权重算法,

 


    /// <summary>
    /// 权重运算
    /// </summary>
    public static class WeightsOper<T>
    {
        /// <summary>
        /// 根据权重随机抽取一个实体
        /// </summary>
        /// <param name="obj">实体列表</param>
        /// <returns>返回被抽取的实体</returns>
        public static object GetWeight(IList<T> obj)
        {
            var list = GenerateWeightList(obj);
            int ranNum = RandomNumber(list.Count - 1);
            var result = list[ranNum];
            return result;
        }

        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        private static int RandomNumber(int length)
        {
            Random rad = new Random();
            return rad.Next(0, length);
        }

        /// <summary>
        /// 生成权重序列表
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <returns>返回数据列表</returns>
        private static IList<T> GenerateWeightList(IList<T> data)
        {          
           int weight = 0;       
           IList<T> list = new List<T>();
           foreach (var one in data)
           {
                Type type = typeof(T);
                var t = type.GetProperties();
                int vweight = 0;
                // Weight 为权重数 只支持整数
                if (int.TryParse(type.GetProperty("Weight").GetValue(one, null).ToString(), out vweight))
                {
                    weight += vweight;
                    for (int i = 0; i < vweight; i++)
                    {
                        list.Add(one);
                    }
                }
            }
            return list;
        }

    }

C#中,带权重的随机选择通常涉及到概率分布和随机数生成。一个常见的方法是使用`System.Collections.Generic.Dictionary<TKey, TValue>`或者`Dictionary<T, double>`来存储元素及其对应的权重,然后利用这些权重来计算每个元素被选中的概率。这里有一个简单的概念实现: ```csharp using System; using System.Collections.Generic; using System.Linq; class WeightedRandomSelection { private readonly Dictionary<int, double> weights; public WeightedRandomSelection(Dictionary<int, double> weights) { this.weights = weights; // 确保所有权重的和为1(或接近于1),以便进行正确的概率计算 double totalWeight = weights.Values.Sum(); if (totalWeight != 1) { weights = weights.ToDictionary(kvp => kvp.Key, kvp => kvp.Value / totalWeight); } } public int Choose() { double randomValue = Random.NextDouble(); // 0到1之间的随机浮点数 double cumulativeProbability = 0; foreach (var (key, weight) in weights) { cumulativeProbability += weight; if (randomValue <= cumulativeProbability) { return key; } } throw new InvalidOperationException("无法生成随机选择,可能是因为权重总和不为1"); } } public class Program { static void Main(string[] args) { Dictionary<int, double> selections = new Dictionary<int, double> { {1, 0.3}, {2, 0.4}, {3, 0.3} }; WeightedRandomSelection random = new WeightedRandomSelection(selections); int chosenNumber = random.Choose(); Console.WriteLine($"随机选择了数字: {chosenNumber}"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值