using UnityEngine; using System.Collections; /// /// This singleton is responsible for keeping an instance of System.Random which we can reset to a specific seed. /// This is important for replaying the exact same sequence of pseudo-random numbers. /// public class RandomSingleton { #region Singleton private static RandomSingleton instance; public static RandomSingleton Instance { get { if (instance == null) { instance = new RandomSingleton(); } return instance; } } #endregion System.Random _random; RandomSingleton() { SetSeed(0); } public void SetSeed(int seed) { AVDebug.Log("Random Singleton Seed set to "+seed); _random = new System.Random(seed); } public int NextInt() { int r = _random.Next(); //AVDebug.Log("!!!Got Random int "+r); return r; } public int NextInt(int min, int max) { AVDebug.Assert(max >= min, "Max must be >= min"); int range = max - min; int r = min + (int)(_random.NextDouble() * range); //AVDebug.Log("!!!Got Random int from range "+r); return r; } public float NextFloat() { float r = (float)_random.NextDouble(); //AVDebug.Log("!!!Got Random float "+r); return r; } public float NextFloat(float min, float max) { AVDebug.Assert(max >= min, "Max must be >= min"); float range = max - min; float r = min + (float)(_random.NextDouble() * range); //AVDebug.Log("!!!Got Random float from range "+r); return r; } }