Esempio n. 1
0
        public static T RandomElement <T>(this IEnumerable <T> e, DeepCloneableRandom rnd)
        {
            IList <T> arr = e as IList <T> ??
                            e.ToArray();

            return(arr[rnd.Next(arr.Count)]);
        }
Esempio n. 2
0
 public static Span <T> Shuffle <T>(this Span <T> list, DeepCloneableRandom rnd)
 {
     for (int i = 0; i < list.Length; i++)
     {
         int r    = rnd.Next(i, list.Length);
         T   temp = list[i];
         list[i] = list[r];
         list[r] = temp;
     }
     return(list);
 }
Esempio n. 3
0
 public static IList <T> Shuffle <T>(this IList <T> list, DeepCloneableRandom rnd)
 {
     for (int i = 0; i < list.Count; i++)
     {
         int r    = rnd.Next(i, list.Count);
         T   temp = list[i];
         list[i] = list[r];
         list[r] = temp;
     }
     return(list);
 }
Esempio n. 4
0
 // The copy constructor.
 private DeepCloneableRandom(DeepCloneableRandom other)
 {
     _state0 = other._state0;
     _state1 = other._state1;
 }
Esempio n. 5
0
 //public static T Choose<T>(this IList<T> list, Random rnd) => list[rnd.Next(list.Count)];
 public static T Choose <T>(this IReadOnlyList <T> list, DeepCloneableRandom rnd) => list[rnd.Next(list.Count)];
Esempio n. 6
0
        public static T[] ChooseNElements <T>(this IReadOnlyList <T> list, int amount, DeepCloneableRandom rnd)
        {
            int c = list.Count;

            if (amount > c)
            {
                amount = c;
            }

            T[] results = new T[amount];

            Span <int> indices = stackalloc int[amount];

            for (int k = 0; k < amount; k++)
            {
                int  j;
                bool flag;
                do
                {
                    j = rnd.Next(c);

                    flag = false;
                    for (int i = 0; i < k; i++)
                    {
                        if (indices[i] == j)
                        {
                            flag = true;
                            break;
                        }
                    }
                } while (flag);

                results[k] = list[j];
                indices[k] = j;
            }


            return(results);
        }