public static int Range(this ZergRandom rand, int min, int max) { if (max <= min) { return(min); } return(min + rand.Next() % (max - min)); }
public static T RandomElement <T>(this ICollection <T> list, ZergRandom random) { if (list.Count < 1) { return(default(T)); } return(list.ElementAt(random.Next(0, list.Count))); }
// max is exclusive index max, count is number of random indexes returned public static int[] RandomNonoverlappedIndices(int max, int count, ZergRandom random) { if (max < count) { int[] r = new int[max]; for (int i = 0; i < max; i++) { r[i] = i; } return(r); } int[] result = new int[count]; var range = Enumerable.Range(0, max).ToList(); for (int i = 0; i < count; ++i) { int randIndex = random.Next(0, max - i); int rand = range[randIndex]; result[i] = rand; range[randIndex] = range[max - i - 1]; } return(result); }