Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. public static string GetRandomValues(string input)
  2. {
  3. Regex rgx = new Regex("\\{[^\\{\\}]*\\}", RegexOptions.IgnoreCase);
  4.  
  5. MatchCollection matches = rgx.Matches(input);
  6.  
  7. foreach (Match match in matches)
  8. {
  9. String total = match.Value;
  10. total = total.Substring(1, total.Length - 1); // Removes "{" and "}"
  11.  
  12. String[] parts = total.Split(new char[] { '|' });
  13. int[] indexWeights = new int[parts.Length];
  14. int totalWeight = 0;
  15.  
  16. for (int i = 0; i < indexWeights.Length; i++) // Initialize
  17. {
  18. indexWeights[i] = -1;
  19. }
  20.  
  21. for (int i = 0; i < parts.Length; i++) // Load set weights
  22. {
  23. String[] split = parts[i].Split(new char[] { '%' }, 2);
  24. if (split.Length >= 2 && int.TryParse(split[0], out int weight))
  25. {
  26. indexWeights[i] = weight;
  27. totalWeight += weight;
  28. parts[i] = split[1];
  29. }
  30. }
  31.  
  32. if (totalWeight < 100 && totalWeight > 0) // If the weights don't need to be generated, it'll skip over this
  33. {
  34. int totalRequired = 0;
  35.  
  36. foreach (int weights in indexWeights)
  37. {
  38. if (weights < 0)
  39. {
  40. totalRequired++;
  41. }
  42. }
  43.  
  44. if (totalRequired > 0 && totalWeight + totalRequired <= 100) // If generating the values is possible, do it
  45. {
  46. int remainingWeight = 100 - totalWeight;
  47.  
  48. for (int i = 0; i < parts.Length; i++) // Distribute remaining weight
  49. {
  50. if (indexWeights[i] < 0)
  51. {
  52. int weight = remainingWeight / totalRequired;
  53. indexWeights[i] = weight;
  54. totalWeight += weight;
  55.  
  56. remainingWeight = 100 - totalWeight;
  57. }
  58. }
  59. }
  60. }
  61. else if (totalWeight == 0) // If the weights were not set at all or are equal to 0
  62. {
  63. for (int i = 0; i < indexWeights.Length; i++) // Set all weights to 1
  64. {
  65. indexWeights[i] = 0;
  66. }
  67. }
  68.  
  69. foreach (int weights in indexWeights) // If any weights are unset
  70. {
  71. if (weights < 0)
  72. {
  73. return "Weights are Unset";
  74. }
  75. }
  76.  
  77. int[] indexWeightsClone = (int[]) indexWeights.Clone();
  78. int cumulativeValue = 0;
  79. int indexChoice = 0;
  80.  
  81. for (int i = 0; i < indexWeightsClone.Length; i++)
  82. {
  83. cumulativeValue += indexWeightsClone[i];
  84. indexWeightsClone[i] = cumulativeValue;
  85. }
  86.  
  87. int randomChoice = UnityEngine.Random.Range(0, cumulativeValue);
  88.  
  89. for (int i = 0; i < indexWeightsClone.Length; i++)
  90. {
  91. if (randomChoice < indexWeightsClone[i])
  92. {
  93. indexChoice = i;
  94. break;
  95. }
  96. }
  97.  
  98. input.Replace(match.Value, parts[indexChoice]);
  99. }
  100.  
  101. return input;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement