RandomService.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class RandomService
  6. {
  7. static RandomHandel s_onRandomCreat;
  8. static bool s_isFixedRandom = false;
  9. static List<int> s_randomList = new List<int>();
  10. public static RandomHandel OnRandomCreat
  11. {
  12. get { return RandomService.s_onRandomCreat; }
  13. set { RandomService.s_onRandomCreat = value; }
  14. }
  15. public static void SetRandomList(List<int> list)
  16. {
  17. s_isFixedRandom = true;
  18. s_randomList = list;
  19. }
  20. public static int GetRandomListCount()
  21. {
  22. return s_randomList.Count;
  23. }
  24. public static int GetRand(int min, int max)
  25. {
  26. return Range(min, max);
  27. }
  28. public static int GetRandReal(int min, int max)
  29. {
  30. return Range(min, max + 1);
  31. }
  32. public static int Range(int min, int max)
  33. {
  34. if (!s_isFixedRandom)
  35. {
  36. int random = UnityEngine.Random.Range(min, max);
  37. DispatchRandom(random);
  38. return random;
  39. }
  40. else
  41. {
  42. return GetFixedRandom();
  43. }
  44. }
  45. public static float Range01()
  46. {
  47. int random = 0;
  48. if (s_isFixedRandom)
  49. {
  50. random = GetFixedRandom();
  51. }
  52. else
  53. {
  54. random = UnityEngine.Random.Range(0, 10001);
  55. DispatchRandom(random);
  56. }
  57. float result = ((float)random) / 10000;
  58. return result;
  59. }
  60. static void DispatchRandom(int random)
  61. {
  62. if (s_onRandomCreat != null)
  63. {
  64. s_onRandomCreat(random);
  65. }
  66. }
  67. static int GetFixedRandom()
  68. {
  69. if (s_randomList != null && s_randomList.Count > 0)
  70. {
  71. int random = s_randomList[0];
  72. s_randomList.RemoveAt(0);
  73. return random;
  74. }
  75. else
  76. {
  77. throw new Exception("RandomService Exception no RandomList!");
  78. }
  79. }
  80. public class FixRandom
  81. {
  82. public int m_RandomSeed = 0;
  83. int m_randomA = 9301;
  84. int m_randomB = 49297;
  85. int m_randomC = 233280;
  86. public FixRandom(int seed)
  87. {
  88. SetFixRandomSeed(seed);
  89. }
  90. public void SetFixRandomSeed(int seed)
  91. {
  92. m_RandomSeed = seed;
  93. }
  94. public void SetFixRandomParm(int a, int b, int c)
  95. {
  96. m_randomA = a;
  97. m_randomB = b;
  98. m_randomC = c;
  99. }
  100. public int GetFixRandom()
  101. {
  102. m_RandomSeed = Math.Abs((m_RandomSeed * m_randomA + m_randomB) % m_randomC);
  103. return m_RandomSeed;
  104. }
  105. public int Range(int min, int max)
  106. {
  107. if (max <= min)
  108. return min;
  109. int random = GetFixRandom();
  110. int range = max - min;
  111. int res = (random % range) + min;
  112. return res;
  113. }
  114. }
  115. }
  116. public delegate void RandomHandel(int random);