12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// 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.
- /// </summary>
- 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;
- }
- }
|