using System; using System.Collections.Generic; using UnityEngine; using Random = UnityEngine.Random; namespace Framework.Support { /// Contains helper methods for randomness public static class RandomHelper { /// Picks one element from the array by random /// Type of elements to select between /// Elements amongst which a random one should be picked /// A random element from the provided choices public static T SelectRandom(T[] choices) { if((choices == null) || (choices.Length == 0)) { return default(T); } return choices[Random.Range(0, choices.Length)]; } /// Picks one element from the list by random /// Type of elements in the list to select between /// Elements amongst which a random one should be picked /// A random element from the provided choices public static T SelectRandom(IList choices) { if((choices == null) || (choices.Count == 0)) { return default(T); } return choices[Random.Range(0, choices.Count)]; } private const float Perigon = Mathf.PI * 2.0f; /// Random generate whose output roughly follows a sine function /// Time for which to generate a random value /// How fast the output will shake /// The random value following a sine function public static float RandomSine(float time, float frequency) { float sine = unwind(time, 0.0f, 1.0f); sine *= Perigon * frequency; // The sine method can cope with *some* loops sine += Random.Range(-0.5f, 0.5f); return Mathf.Sin(sine); } /// Unwinds a value in a circular (repeating) value range /// Value that will be unwound /// Lower limit of the circular value range /// Upper limit of the circular value range /// The unwound value private static float unwind(float value, float min, float max) { float range = (max - min); float floor = Mathf.Floor((value - min) / range); return value - (floor * range); } } } // namespace Framework.Support