/// <summary> /// Creates a random string from the source data up until "size" chars. /// </summary> /// <param name="source"></param> /// <param name="size"></param> /// <returns></returns> public static string GetRandom(this string source, int size) { var result = ""; var i = 0; while (i < size) { result += source[RngKit.Next(0, source.Length)]; i++; } return(result); }
public static List <T1> FillRandom <T1>(this List <T1> source, Func <int, T1> fn, int from, int to) { // Design 1: Map the range to a data structure, eliminate the ones who are already in the lottery. Saves random time. Might not be suitable for large collections. // Design 2: Continuously RNG, discard what has already showed up. Saves memory. Wastes randomness. // Design 3: Discard objects from the real collection (or a copy), remove and rng again. Rinse and repeat. var collisionSet = new HashSet <int>(); var distance = to - from >= 0 ? to - from : from - to; while (collisionSet.Count < distance) { var dice = RngKit.Next(from, to + 1); if (collisionSet.Contains(dice)) { continue; } collisionSet.Add(dice); source.Add(fn(dice)); } return(source); }
public static int PickRandomIndex <T>(this IList <T> set) { return(RngKit.Next(set.Count)); }
public static T PickRandomElement <T>(this IList <T> set, int min) { return(set[RngKit.Next(min, set.Count)]); }