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)]); }
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); }
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); }
// The copy constructor. private DeepCloneableRandom(DeepCloneableRandom other) { _state0 = other._state0; _state1 = other._state1; }
//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)];
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); }