public static Vector2Int Vector2Int(this IRNG rng, int xMin, int xMax, int yMin, int yMax) { return(new Vector2Int { x = rng.Range(xMin, xMax), y = rng.Range(yMin, yMax) }); }
public static Vector2 InCircle(this IRNG random, Vector2 center, float radius) { var x = random.Range(-1f, 1f); var y = random.Range(-1f, 1f); var direction = new Vector2(x, y).normalized; var delta = direction * random.Range(radius); return(center + delta); }
/// <summary> /// Chooses a random valid index for the <paramref name="list"/>. /// </summary> /// <typeparam name="T">Type of the elements in the enumerable.</typeparam> /// <param name="list">The enumerable to choose an index for.</param> /// <param name="random">Random number generator.</param> /// <returns>A valid index for the enumerable.</returns> public static int ChooseRandomIndex <T>(this IList <T> list, IRNG random = null) { var indexMax = list.Count; random ??= uRandom; return(list.Count > 0 ? random.Range(indexMax) : -1); }
public static IEnumerable <int> RandomIndices <T>(this IList <T> list, IRNG random = null) { var maxIndex = list.Count; random ??= uRandom; while (true) { yield return(random.Range(maxIndex)); } }
public HSVColor Generate(float hue, float value, IRNG random) { var saturation = random.Range(this.saturationRange); return(new HSVColor { Hue = hue, Saturation = saturation, Value = value }); }
public T Sample(IRNG random) { var randomValue = random.Range(0, this.weightSum); foreach (var itemWeightPair in this.weights) { randomValue -= itemWeightPair.Value; if (randomValue <= 0) { return(itemWeightPair.Key); } } return(this.weights.Keys.Last()); }
public T Sample(IRNG random) { var weightTotal = this.weightedItems.Values.Sum(); var price = random.Range(0, weightTotal); var items = this.weightedItems.Keys.RandomOrder(random).ToArray(); foreach (var item in items) { var weight = this.weightedItems[item]; price -= weight; if (price <= 0) { return(item); } } return(items.Last()); }
public static float Range(this IRNG rng, Vector2 range) => rng.Range(range.x, range.y);
public static float Range(this IRNG rng, float max) { rng ??= UnityRandom.Shared; return(rng.Range(0, max)); }
public static int Range(this IRNG rng, int max, bool inclusive = false) { return(rng.Range(0, max, inclusive)); }
public static int ChooseIndex <T>(this IRNG random, IReadOnlyCollection <T> list) => random.Range(0, list.Count);
public static int ChooseIndex <T>(this IRNG random, IList <T> list) => random.Range(0, list.Count);
/// <summary> /// Generates a random value between the <paramref name="range"/>.x as the lower bound and the <paramref name="range"/>.y as the upper bound. /// </summary> public static float RandomInRange(this Vector2 range, IRNG random = null) { random ??= UnityRandom.Shared; return(random.Range(range.x, range.y)); }
public int Sample(IRNG random) { var max = this.inclusive ? this.maximum + 1 : this.maximum; return(random.Range(this.minimum, max)); }
public HSVColor Generate(float hue, IRNG random) { var value = random.Range(this.valueRange); return(this.Generate(hue, value, random)); }