加權隨機算法

public class WeightedRandom { /// <summary>
            /// 帶權重的隨機 /// </summary>
            /// <param name="list">原始列表</param>
            /// <param name="count">隨機抽取條數</param>
            /// <returns></returns>
            public static T GetRandomList<T>(List<T> list, int count) where T : RandomObject { if (list == null || list.Count <= count || count <= 0) { return default(T); } //計算權重總和
                int totalWeights = 0; for (int i = 0; i < list.Count; i++) { totalWeights += list[i].Weight + 1;  //權重+1,防止爲0狀況。
 } //隨機賦值權重
                System.Random ran = new System.Random(GetRandomSeed());  //GetRandomSeed()隨機種子,防止快速頻繁調用致使隨機同樣的問題 
                List<KeyValuePair<int, int>> wlist = new List<KeyValuePair<int, int>>();    //第一個int爲list下標索引、第一個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; }); //根據實際狀況取排在最前面的幾個
                List<T> newList = new List<T>(); for (int i = 0; i < count; i++) { newList.Add(list[wlist[i].Key]); } //隨機法則
                return newList[0]; } /// <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); } } public class RandomObject { /// <summary>
            /// 權重 /// </summary>
            public int Weight { set; get; } } public class GameItemRandomObject : RandomObject { public int item { get; set; } } static void Test6() { List<GameItemRandomObject> shuidiRanObj = new List<GameItemRandomObject>(); shuidiRanObj.Add(new GameItemRandomObject() { item = 0, Weight = 15 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 1, Weight = 30 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 2, Weight = 1 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 3, Weight = 5 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 4, Weight = 2 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 5, Weight = 12 }); shuidiRanObj.Add(new GameItemRandomObject() { item = 6, Weight = 6 }); var CurItem = WeightedRandom.GetRandomList<GameItemRandomObject>(shuidiRanObj, 1); }
相關文章
相關標籤/搜索