Advertisement
Guest User

SelectFromList

a guest
Apr 19th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. public static List<T> SelectRandom<T>(List<T> SomeList, int Need)
  2. {
  3.     int NeedSelect = Need;
  4.  
  5.     if (NeedSelect > SomeList.Count) { return new List<T>(SomeList); }
  6.     if (NeedSelect <= 0) { new List<T>(); }
  7.  
  8.     List<T> Result = new List<T>(NeedSelect);
  9.  
  10.     while (NeedSelect > 0)
  11.     {
  12.         // дроп листа
  13.         Result.Clear();
  14.         // восстановление числа выборок
  15.         NeedSelect = Need;
  16.  
  17.         for (int i = 0; i < SomeList.Count; i++)
  18.         {
  19.             if (DropDice(NeedSelect, (SomeList.Count - Result.Count)))
  20.             {
  21.                 NeedSelect--;
  22.                 Result.Add(SomeList[i]);
  23.  
  24.                 if (NeedSelect == 0) break;
  25.             }
  26.         }
  27.     }
  28.  
  29.     return Result;
  30. }
  31.  
  32. public static List<T> SelectRandomExclude<T>(List<T> SomeList, int Need)
  33. {
  34.     int NeedExclude = SomeList.Count - Need;
  35.  
  36.     if (NeedExclude > SomeList.Count) { return new List<T>(); }
  37.     if (NeedExclude <= 0) { return new List<T>(SomeList);  }
  38.  
  39.     // копирование
  40.     List<T> Result = new List<T>(SomeList);
  41.  
  42.     while (NeedExclude > 0)
  43.     {
  44.         for (int i = Result.Count - 1; i >=0 ; i--)
  45.         {
  46.             if (DropDice(NeedExclude, Result.Count))
  47.             {
  48.                 NeedExclude--;
  49.                 Result.RemoveAt(i);
  50.  
  51.                 if (NeedExclude == 0) break;
  52.             }
  53.         }
  54.     }
  55.  
  56.     return Result;
  57. }
  58.  
  59. private static bool DropDice(int Need, int Avaiable)
  60. {
  61.     return (DevilEngine.Tools.RandomGlobal.SatanGlobalRandomGenerator.NextDouble() < ((float)Need / (float)Avaiable));
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement