public static T GetRandomElementWithPredicate <T>( this IReadOnlyList <T> e, Func <T, bool> predicate) { if (e.Count == 0) { return(default(T)); } int maxValue = 0; for (int index = 0; index < e.Count; ++index) { if (predicate(e[index])) { ++maxValue; } } if (maxValue == 0) { return(default(T)); } int num = MBRandom.RandomInt(maxValue); for (int index = 0; index < e.Count; ++index) { if (predicate(e[index])) { --num; if (num < 0) { return(e[index]); } } } return(default(T)); }
public static T GetRandomElementWeighted <T>( this IReadOnlyList <T> e, Func <T, float> randomWeights) { return(e.Count != 0 ? MBRandom.ChooseWeighted <T>((IEnumerable <T>)e, randomWeights) : throw new ArgumentException()); }
public static T GetRandomElementInefficiently <T>(this IEnumerable <T> e) => e.IsEmpty <T>() ? default(T) : e.ElementAt <T>(MBRandom.RandomInt(e.Count <T>()));
public static T GetRandomElement <T>(this T[] e) => e.Length == 0 ? default(T) : e[MBRandom.RandomInt(e.Length)];
public static T GetRandomElement <T>(this IReadOnlyList <T> e) => e.Count == 0 ? default(T) : e[MBRandom.RandomInt(e.Count)];
public static string GetRandomMountKey(ItemObject mountItem, int randomSeed) { Random random = new Random(randomSeed); if (mountItem == null) { return(new MountCreationKey((byte)random.Next(4), (byte)random.Next(4), (byte)random.Next(4), (byte)random.Next(4), (byte)0, (byte)0).ToString()); } HorseComponent horseComponent = mountItem.HorseComponent; if (horseComponent.HorseMaterialNames == null || horseComponent.HorseMaterialNames.Count <= 0) { return(new MountCreationKey((byte)MBRandom.RandomInt(4), (byte)MBRandom.RandomInt(4), (byte)MBRandom.RandomInt(4), (byte)MBRandom.RandomInt(4), (byte)0, (byte)0).ToString()); } int index1 = random.Next(horseComponent.HorseMaterialNames.Count); float num1 = random.NextFloat(); int num2 = 0; float num3 = 0.0f; HorseComponent.MaterialProperty horseMaterialName = horseComponent.HorseMaterialNames[index1]; for (int index2 = 0; index2 < horseMaterialName.MeshMultiplier.Count; ++index2) { num3 += horseMaterialName.MeshMultiplier[index2].Item2; if ((double)num1 <= (double)num3) { num2 = index2; break; } } return(new MountCreationKey((byte)random.Next(4), (byte)random.Next(4), (byte)random.Next(4), (byte)random.Next(4), (byte)index1, (byte)num2).ToString()); }
public static T ChooseWeighted <T>(IEnumerable <T> candidates, Func <T, float> weightFunction) => MBRandom.ChooseWeighted <T>(candidates, weightFunction, out int _);