random_ScriptBinding.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mNormalDistribution.h"
  23. ConsoleFunctionGroupBegin(RandomNumbers, "Functions relating to the generation of random numbers.");
  24. /*! @defgroup RandomNumbersFunctions Random Numbers
  25. @ingroup TorqueScriptFunctions
  26. @{
  27. */
  28. /*! Use the setRandomSeed function to initialize the random number generator with the initial seed of startSeed.
  29. @param startSeed The new starting seed value for the random generator.
  30. @return No return value.
  31. @sa getRandom, getRandomSeed
  32. */
  33. ConsoleFunctionWithDocs(setRandomSeed, ConsoleVoid, 1, 2, ( startSeed ))
  34. {
  35. U32 seed = Platform::getRealMilliseconds();
  36. if (argc == 2)
  37. seed = dAtoi(argv[1]);
  38. RandomLCG::setGlobalRandSeed(seed);
  39. }
  40. /*! Use the getRandomSeed function to get the current seed for the random generator.
  41. You can re-seed the generator with this value to allow you to recreate a random sequence. Merely save the seed and execute your random sequence. Later, to reproduce this sequence re-seed the generator with setRandomSeed and your saved value. Then, the generator will produce the same random sequence that followed the call to getRandomSeed.
  42. @return Returns an integer value representing the current seed of the random generator.
  43. @sa getRandom, setRandomSeed
  44. */
  45. ConsoleFunctionWithDocs(getRandomSeed, ConsoleInt, 1, 1, ())
  46. {
  47. return gRandGen.getSeed();
  48. }
  49. S32 mRandI( const S32 i1, const S32 i2)
  50. {
  51. return gRandGen.randRangeI(i1, i2);
  52. }
  53. F32 mRandF(const F32 f1, const F32 f2)
  54. {
  55. return gRandGen.randRangeF(f1, f2);
  56. }
  57. /*! Use the getRandom function to get a random floating-point or integer value. This function comes in three forms.
  58. The first getRandom() takes no arguments and will return a random floating-point value in the range of 0.0 to 1.0. The second getRandom( max ) takes one argument representing the max integer value this will return. It will return an integer value between 0 and max. The third getRandom( min , max ) takes two arguments representing the min and max integer values this will return. It will return an integer value between min and max. Only the no-args version will return a floating point.
  59. @param min Minimum inclusive integer value to return.
  60. @param max Maximum inclusive integer value to return.
  61. @return If no arguments are passed, will return a floating-point value between 0.0 and 1.0. If one argument is passed, will return an integer value between 0 and max inclusive. If two arguments are passed, will return an integer value between min and max inclusive.
  62. @sa getRandomSeed
  63. */
  64. ConsoleFunctionWithDocs(getRandom, ConsoleFloat, 1, 3, ([ min ]?,[ max ]?))
  65. {
  66. if (argc == 2)
  67. return F32(gRandGen.randRangeI(0,getMax( dAtoi(argv[1]), 0 )));
  68. else
  69. {
  70. if (argc == 3)
  71. {
  72. S32 min = dAtoi(argv[1]);
  73. S32 max = dAtoi(argv[2]);
  74. if (min > max)
  75. {
  76. S32 t = min;
  77. min = max;
  78. max = t;
  79. }
  80. return F32(gRandGen.randRangeI(min,max));
  81. }
  82. }
  83. return gRandGen.randF();
  84. }
  85. /*! Gets a random floating-point number from min to max.
  86. @param min The minimum range of the random floating-point number.
  87. @param max The maximum range of the random floating-point number.
  88. @return A random floating-point number from min to max.
  89. */
  90. ConsoleFunctionWithDocs(getRandomF, ConsoleFloat, 3, 3, (min, max))
  91. {
  92. F32 min = dAtof(argv[1]);
  93. F32 max = dAtof(argv[2]);
  94. if ( min > max )
  95. {
  96. const F32 temp = min;
  97. min = max;
  98. max = temp;
  99. }
  100. return min + (gRandGen.randF() * (max-min));
  101. }
  102. /*! Gets a random integer number with normal distribution (a bell curve) from min to max with optional mean and standard deviation.
  103. @param min The minimum range of the random integer number.
  104. @param max The maximum range of the random integer number.
  105. @param mean The Most commonly occuring value or the center of the "bell". Must be between the min and max. Defaults to the center value between min and max.
  106. @param stdDev The standard deviation affects how far values tend to be from the mean. Defaults to 1/6th of the difference between min and max.
  107. @return A random floating-point number from min to max.
  108. */
  109. ConsoleFunctionWithDocs(getRandomBell, ConsoleInt, 3, 5, (min, max, [ mean ]?, [ stdDev ]?))
  110. {
  111. S32 min = dAtoi(argv[1]);
  112. S32 max = dAtoi(argv[2]);
  113. if (min > max)
  114. {
  115. const S32 temp = min;
  116. min = max;
  117. max = temp;
  118. }
  119. if (argc == 3)
  120. {
  121. NormalDistributionGenerator g(min, max);
  122. return g();
  123. }
  124. else if (argc > 3 && argc <= 5)
  125. {
  126. S32 mean = dAtoi(argv[3]);
  127. if (mean > max)
  128. {
  129. mean = max;
  130. }
  131. if (mean < min)
  132. {
  133. mean = min;
  134. }
  135. if (argc == 4)
  136. {
  137. NormalDistributionGenerator g(min, max, mean);
  138. return g();
  139. }
  140. else
  141. {
  142. NormalDistributionGenerator g(min, max, mean, dAtoi(argv[4]));
  143. return g();
  144. }
  145. }
  146. else
  147. {
  148. Con::warnf("getRandomBell() - Invalid number of parameters!");
  149. return 0;
  150. }
  151. }
  152. //------------------------------------------------------------------------------
  153. ConsoleFunctionGroupEnd(RandomNumbers)
  154. /*! @} */ // group RandomNumberFunctions