Exemple #1
0
        public static List <T> Sample <T>(this IList <T> self, bool replacement = false, int sampleSize = -1, IRandom random = null)
        {
            if (sampleSize == -1)
            {
                sampleSize = self.Count;
            }

            if (replacement)
            {
                random ??= RandomInstance.Get();
                var result = new List <T>(sampleSize);
                for (var i = 0; i < sampleSize; ++i)
                {
                    var j = random.Next(self.Count);
                    result.Add(self[j]);
                }

                return(result);
            }
            else
            {
                if (sampleSize > self.Count)
                {
                    throw new ArgumentOutOfRangeException("sampleSize", "Sample size must not be larger than the size of the population without replacement.");
                }

                var result = Shuffle(self, random);
                if (sampleSize == self.Count)
                {
                    return(result);
                }
                return(result.Take(sampleSize).ToList());
            }
        }
Exemple #2
0
 public static void ShuffleFill <T>(this IList <T> self, IRandom random = null)
 {
     random ??= RandomInstance.Get();
     for (var i = 0; i < self.Count; ++i)
     {
         var j    = random.Next(i + 1);
         var temp = self[i];
         self[i] = self[j];
         self[j] = temp;
     }
 }
Exemple #3
0
        public static List <T> Shuffle <T>(this IList <T> self, IRandom random = null)
        {
            // ref: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

            var result = new List <T>(self.Count);

            random ??= RandomInstance.Get();
            for (var i = 0; i < self.Count; ++i)
            {
                var j = random.Next(i + 1);
                if (j == i)
                {
                    result.Add(self[i]);
                }
                else
                {
                    result.Add(result[j]);
                    result[j] = self[i];
                }
            }

            return(result);
        }