public static List <T> RandomPickN(List <T> dataArray, uint count)//等概率 { if (count == 0) { return(new List <T>()); } if (dataArray.Count <= count)//全部掉出 { return(dataArray); } NLRandomPicker <T> picker = new NLRandomPicker <T>(); for (int i = 0; i < dataArray.Count; i++) { picker.AddData(dataArray[i], 1); } return(new List <T>(picker.RandomPickN(count))); }
/// <summary> /// 从dataArray数组中按照概率rateArray,随机选出count个物件 /// </summary> /// <param name="dataArray">样本数组</param> /// <param name="rateArray">概率数组</param> /// <param name="count">选取数</param> /// <returns>返回选取的物件</returns> public static T[] RandomPickN(T[] dataArray, float[] rateArray, uint count) { if (dataArray.Length != rateArray.Length || count == 0) { return(new T[0]); } if (dataArray.Length <= count)//全部掉出 { return(dataArray); } //todo 考虑下n选m,时m>n/2,做减法 NLRandomPicker <T> picker = new NLRandomPicker <T>(); for (int i = 0; i < dataArray.Length; i++) { picker.AddData(dataArray[i], rateArray[i]); } return(picker.RandomPickN(count)); }
/// <summary> /// 从dataArray数组中以等概率随机选出count个物件 /// </summary> /// <param name="dataArray">样本数组</param> /// <param name="count">选取数</param> /// <returns>返回选取的物件</returns> public static T[] RandomPickN(T[] dataArray, uint count)//等概率 { if (count == 0) { return(new T[0]); } if (dataArray.Length <= count)//全部掉出 { return(dataArray); } NLRandomPicker <T> picker = new NLRandomPicker <T>(); for (int i = 0; i < dataArray.Length; i++) { picker.AddData(dataArray[i], 1); } return(picker.RandomPickN(count)); }