随机抽取算法

 

 

    /// <summary>
    /// 随机抽取
    /// </summary>
    /// <param name="dataList">直接传输每种类型的个数的List,比如0号50个,1号10个,2号20个</param>
   /// 示例: List<int> _list = new List<int> {50,10,20}; /// <returns>最终返回为抽取到了哪个编号</returns> public static int RandomGetIndex(List<int> dataList) { int maxNum = 0; foreach (var one in dataList) { maxNum += one; } //int randomNum = RandomHelper.RandomNumber(0, maxNum); int randomNum = UnityEngine.Random.Range(0, maxNum); int weightNum = 0; for (int i = 0; i < dataList.Count; i++) { weightNum += dataList[i]; if (randomNum < weightNum) { return i; } } return 0; }

 

posted @ 2020-05-17 10:36  三页菌  阅读(58)  评论(0编辑  收藏