RandomSequence.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 
  2. // =================================
  3. // Namespaces.
  4. // =================================
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. // =================================
  8. // Define namespace.
  9. // =================================
  10. namespace MirzaBeig
  11. {
  12. namespace Demos
  13. {
  14. namespace TheLastParticle
  15. {
  16. // =================================
  17. // Classes.
  18. // =================================
  19. //[ExecuteInEditMode]
  20. [System.Serializable]
  21. public class RandomSequence
  22. {
  23. // =================================
  24. // Nested classes and structures.
  25. // =================================
  26. // ...
  27. // =================================
  28. // Variables.
  29. // =================================
  30. // ...
  31. int currentIndex;
  32. int[] randomSequence;
  33. // =================================
  34. // Functions.
  35. // =================================
  36. // NON-REPEATING random sequence from range [0, rangeMax] repeated 'loopCount' number of times.
  37. // Total size of returned array = rangeMax * length.
  38. public RandomSequence(int rangeMax, int loopCount)
  39. {
  40. int lastRandomNumber = -1;
  41. randomSequence = new int[rangeMax * loopCount];
  42. int count = 0;
  43. for (int i = 0; i < loopCount; i++)
  44. {
  45. List<int> intList = new List<int>();
  46. for (int j = 0; j < rangeMax; j++)
  47. {
  48. intList.Add(j);
  49. }
  50. for (int j = 0; j < rangeMax; j++)
  51. {
  52. int index = Random.Range(0, intList.Count);
  53. int randomNumber = intList[index];
  54. // When the pattern repeats, the last random number
  55. // may still be selected again since the sequence was
  56. // refreshed.
  57. // Example: [0, 2, 1] and then [1, 2, 0].
  58. // In this case, although each pattern has no repeat,
  59. // the total array ends up being [0, 2, 1, 1, 2, 0].
  60. // This can only happen if both the loopCount and rangeMax
  61. // values are at least 2 and if on the first pick of the
  62. // iteration following the previous (i > 0 && j == 0).
  63. // I only check if j == 0 because of the number of calculations
  64. // avoided by the if-statement is best this way.
  65. // If rangeMax is 1, then of course repeating is unavoidable
  66. // because there's only one number to pick from, so ignore if
  67. // range max is not > 1. Else, just cycle to the next number
  68. // to make sure it won't be the same as the last number selected.
  69. // If there's only one number, the sequence will always be [0, 0, 0, 0...].
  70. // If there's only two numbers, the sequence will always be [0, 1, 0, 1...].
  71. if (j == 0)
  72. {
  73. if (randomNumber == lastRandomNumber && rangeMax > 1)
  74. {
  75. index = (index + 1) % intList.Count;
  76. randomNumber = intList[index];
  77. }
  78. }
  79. randomSequence[count] = randomNumber;
  80. lastRandomNumber = randomNumber;
  81. intList.RemoveAt(index); count++;
  82. }
  83. }
  84. }
  85. public int get()
  86. {
  87. int value = randomSequence[currentIndex];
  88. currentIndex = (currentIndex + 1) % randomSequence.Length;
  89. return value;
  90. }
  91. // =================================
  92. // End functions.
  93. // =================================
  94. }
  95. // =================================
  96. // End namespace.
  97. // =================================
  98. }
  99. }
  100. }
  101. // =================================
  102. // --END-- //
  103. // =================================