Ejemplo n.º 1
0
 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)
     });
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
0
        public HSVColor Generate(float hue, float value, IRNG random)
        {
            var saturation = random.Range(this.saturationRange);

            return(new HSVColor
            {
                Hue = hue,
                Saturation = saturation,
                Value = value
            });
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
        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());
        }
Ejemplo n.º 8
0
 public static float Range(this IRNG rng, Vector2 range) => rng.Range(range.x, range.y);
Ejemplo n.º 9
0
 public static float Range(this IRNG rng, float max)
 {
     rng ??= UnityRandom.Shared;
     return(rng.Range(0, max));
 }
Ejemplo n.º 10
0
 public static int Range(this IRNG rng, int max, bool inclusive = false)
 {
     return(rng.Range(0, max, inclusive));
 }
Ejemplo n.º 11
0
 public static int ChooseIndex <T>(this IRNG random, IReadOnlyCollection <T> list) => random.Range(0, list.Count);
Ejemplo n.º 12
0
 public static int ChooseIndex <T>(this IRNG random, IList <T> list) => random.Range(0, list.Count);
Ejemplo n.º 13
0
 /// <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));
 }
Ejemplo n.º 14
0
        public int Sample(IRNG random)
        {
            var max = this.inclusive ? this.maximum + 1 : this.maximum;

            return(random.Range(this.minimum, max));
        }
Ejemplo n.º 15
0
        public HSVColor Generate(float hue, IRNG random)
        {
            var value = random.Range(this.valueRange);

            return(this.Generate(hue, value, random));
        }