算法——权重随机

博客围绕随机相关算法展开,但具体内容缺失。随机算法在信息技术领域有广泛应用,可用于数据处理、模拟等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /// <summary>
    /// 权重对象
    /// </summary>
    public class RandomObject
    {
        /// <summary>
        /// 权重
        /// </summary>
        public int Weight { set; get; }
        /// <summary>
        /// Id
        /// </summary>
        public int Id { get; set; }
    }
    /// <summary>
    /// 四类宝箱中物品数量的权重(json格式数据):
    /// [[1,101],[2,102],[31,103],[41,104]],
    /// 宝箱中具体物品数量的权重(json格式数据):
    /// [[1202,100],[1203,100],[1201,100],[1210,100]
    /// ,[1204,101],[1205,101],[1206,102],[1208,102],
    /// [1207,103],[1209,103]]
    /// 物品所属宝箱类型:
    /// 1201   31
    /// 1202   1
    /// 1203   1
    /// 1204   1
    /// 1205   2
    /// 1206   1
    /// 1207   1
    /// 1208   2
    /// 1209   2
    /// 1210   41
    /// 说明:[1,101] 中 1表示宝箱ID, 
    /// 101表示宝箱中物品总数量的权重。
    /// [1202,100]中 1202表示宝箱中具体物品的ID, -
    /// ..-0
    /// 
    ///  现在有39个物品分布在4类宝箱中,
    ///  出现的个数由宝箱权重与具体的物品权重来控制 
    ///  请写出得到每个物品被分配个数的算法
    /// </summary>
    class Program
    {
        //泛型约束 → where T :RandomObject(T 类型为RandomObject 类型 或者 RandomObject的继承)
        public static T GetRandomList<T>(List<T> list, int count) where T : RandomObject
        {
            //计算权重总和
            int totalWeights = 0;
            for (int i = 0; i < list.Count; i++)
            {
                //权重+1,防止为0情况。
                totalWeights += list[i].Weight + 1;
            }
            //随机赋值权重
            //GetRandomSeed()随机种子, 防止快速频繁调用导致随机一样  的问题 
            //如果没有参数 是根据时间
            Random ran = new Random(GetRandomSeed());
            //第一个int为list下标索引、第二个int为权重排序值
            //轻量级键值对集合(KeyValuePair)
            List<KeyValuePair<int, int>> wlist = new List<KeyValuePair<int, int>>();
            for (int i = 0; i < list.Count; i++)
            {
                int w = (list[i].Weight + 1) + ran.Next(0, totalWeights);   // (权重+1) + 从0到(总权重-1)的随机数
                wlist.Add(new KeyValuePair<int, int>(i, w));
            }
            //排序
            wlist.Sort(
              delegate (KeyValuePair<int, int> kvp1, KeyValuePair<int, int> kvp2)
              {
                  return kvp2.Value - kvp1.Value;
              });
            //随机法则
            return list[wlist[0].Key];
        }
        /// <summary>
        /// 随机种子值
        /// </summary>
        /// <returns></returns>
        private static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        static void Main(string[] args)
        {
            List<RandomObject> list = new List<RandomObject>();
            list.Add(new RandomObject { Id = 1, Weight = 101 });
            list.Add(new RandomObject { Id = 2, Weight = 102 });
            list.Add(new RandomObject { Id = 31, Weight = 103 });
            list.Add(new RandomObject { Id = 41, Weight = 104 });
            List<RandomObject> listRes = new List<RandomObject>();
            for (int i = 0; i < 39; i++)
            {
                RandomObject lr = GetRandomList<RandomObject>(list, 1);
                listRes.Add(lr);
            }
            List<RandomObject> list1 = new List<RandomObject>();
            list1.Add(new RandomObject { Id = 1202, Weight = 100 });
            list1.Add(new RandomObject { Id = 1203, Weight = 100 });
            list1.Add(new RandomObject { Id = 1204, Weight = 101 });
            list1.Add(new RandomObject { Id = 1206, Weight = 102 });
            list1.Add(new RandomObject { Id = 1207, Weight = 103 });
            List<RandomObject> listRes1 = new List<RandomObject>();
            int count1 = listRes.FindAll(m => m.Id == 1).Count;
            for (int i = 0; i < count1; i++)
            {
                RandomObject lr = GetRandomList<RandomObject>(list1, 1);
                listRes1.Add(lr);
            }
            Console.WriteLine("Id为1的箱子放入的物品总数量为:" + listRes1.Count);
            foreach (var item in listRes1)
            {
                Console.WriteLine(item.Id);
            }
            List<RandomObject> list2 = new List<RandomObject>();
            list2.Add(new RandomObject { Id = 1205, Weight = 101 });
            list2.Add(new RandomObject { Id = 1208, Weight = 102 });
            list2.Add(new RandomObject { Id = 1209, Weight = 103 });
            List<RandomObject> listRes2 = new List<RandomObject>();
            int count2 = listRes.FindAll(m => m.Id == 2).Count;
            for (int i = 0; i < count2; i++)
            {
                RandomObject lr = GetRandomList<RandomObject>(list2, 1);
                listRes2.Add(lr);
            }
            Console.WriteLine("Id为2的箱子放入的物品总数量为:" + listRes2.Count);
            foreach (var item in listRes2)
            { 
                Console.WriteLine(item.Id);
            }
            List<RandomObject> list3 = new List<RandomObject>();
            list3.Add(new RandomObject { Id = 1201, Weight = 100 });

            List<RandomObject> listRes3 = new List<RandomObject>();
            int count3 = listRes.FindAll(m => m.Id == 31).Count;
            for (int i = 0; i < count3; i++)
            {
                RandomObject lr = GetRandomList<RandomObject>(list3, 1);
                listRes3.Add(lr);
            }
            Console.WriteLine("Id为31的箱子放入的物品总数量为:" + listRes3.Count);
            foreach (var item in listRes3)
            {
                Console.WriteLine(item.Id);
            }
            Console.WriteLine("Id为41的箱子放入的物品总数量为:" + (39 - listRes1.Count - listRes2.Count - listRes3.Count));
            for (int i = 0; i < 39 - listRes1.Count - listRes2.Count - listRes3.Count; i++)
            {
                Console.WriteLine(1210);
            }
            Console.ReadKey();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值