Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<T> SelectRandom<T>(List<T> SomeList, int Need)
- {
- int NeedSelect = Need;
- if (NeedSelect > SomeList.Count) { return new List<T>(SomeList); }
- if (NeedSelect <= 0) { new List<T>(); }
- List<T> Result = new List<T>(NeedSelect);
- while (NeedSelect > 0)
- {
- // дроп листа
- Result.Clear();
- // восстановление числа выборок
- NeedSelect = Need;
- for (int i = 0; i < SomeList.Count; i++)
- {
- if (DropDice(NeedSelect, (SomeList.Count - Result.Count)))
- {
- NeedSelect--;
- Result.Add(SomeList[i]);
- if (NeedSelect == 0) break;
- }
- }
- }
- return Result;
- }
- public static List<T> SelectRandomExclude<T>(List<T> SomeList, int Need)
- {
- int NeedExclude = SomeList.Count - Need;
- if (NeedExclude > SomeList.Count) { return new List<T>(); }
- if (NeedExclude <= 0) { return new List<T>(SomeList); }
- // копирование
- List<T> Result = new List<T>(SomeList);
- while (NeedExclude > 0)
- {
- for (int i = Result.Count - 1; i >=0 ; i--)
- {
- if (DropDice(NeedExclude, Result.Count))
- {
- NeedExclude--;
- Result.RemoveAt(i);
- if (NeedExclude == 0) break;
- }
- }
- }
- return Result;
- }
- private static bool DropDice(int Need, int Avaiable)
- {
- return (DevilEngine.Tools.RandomGlobal.SatanGlobalRandomGenerator.NextDouble() < ((float)Need / (float)Avaiable));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement