2
0

random_ScriptBinding.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ConsoleFunctionGroupBegin(RandomNumbers, "Functions relating to the generation of random numbers.");
  23. /*! @defgroup RandomNumbersFunctions Random Numbers
  24. @ingroup TorqueScriptFunctions
  25. @{
  26. */
  27. /*! Use the setRandomSeed function to initialize the random number generator with the initial seed of startSeed.
  28. @param startSeed The new starting seed value for the random generator.
  29. @return No return value.
  30. @sa getRandom, getRandomSeed
  31. */
  32. ConsoleFunctionWithDocs(setRandomSeed, ConsoleVoid, 1, 2, ( startSeed ))
  33. {
  34. U32 seed = Platform::getRealMilliseconds();
  35. if (argc == 2)
  36. seed = dAtoi(argv[1]);
  37. RandomLCG::setGlobalRandSeed(seed);
  38. }
  39. /*! Use the getRandomSeed function to get the current seed for the random generator.
  40. 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.
  41. @return Returns an integer value representing the current seed of the random generator.
  42. @sa getRandom, setRandomSeed
  43. */
  44. ConsoleFunctionWithDocs(getRandomSeed, ConsoleInt, 1, 1, ())
  45. {
  46. return gRandGen.getSeed();
  47. }
  48. S32 mRandI( const S32 i1, const S32 i2)
  49. {
  50. return gRandGen.randRangeI(i1, i2);
  51. }
  52. F32 mRandF(const F32 f1, const F32 f2)
  53. {
  54. return gRandGen.randRangeF(f1, f2);
  55. }
  56. /*! Use the getRandom function to get a random floating-point or integer value. This function comes in three forms.
  57. 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.
  58. @param min Minimum inclusive integer value to return.
  59. @param max Maximum inclusive integer value to return.
  60. @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.
  61. @sa getRandomSeed
  62. */
  63. ConsoleFunctionWithDocs(getRandom, ConsoleFloat, 1, 3, ([ min ]?,[ max ]?))
  64. {
  65. if (argc == 2)
  66. return F32(gRandGen.randRangeI(0,getMax( dAtoi(argv[1]), 0 )));
  67. else
  68. {
  69. if (argc == 3)
  70. {
  71. S32 min = dAtoi(argv[1]);
  72. S32 max = dAtoi(argv[2]);
  73. if (min > max)
  74. {
  75. S32 t = min;
  76. min = max;
  77. max = t;
  78. }
  79. return F32(gRandGen.randRangeI(min,max));
  80. }
  81. }
  82. return gRandGen.randF();
  83. }
  84. /*! Gets a random floating-point number from min to max.
  85. @param min The minimum range of the random floating-point number.
  86. @param max The maximum range of the random floating-point number.
  87. @return A random floating-point number from min to max.
  88. */
  89. ConsoleFunctionWithDocs(getRandomF, ConsoleFloat, 3, 3, (min, max))
  90. {
  91. F32 min = dAtof(argv[1]);
  92. F32 max = dAtof(argv[2]);
  93. if ( min > max )
  94. {
  95. const F32 temp = min;
  96. min = max;
  97. max = temp;
  98. }
  99. return min + (gRandGen.randF() * (max-min));
  100. }
  101. //------------------------------------------------------------------------------
  102. ConsoleFunctionGroupEnd(RandomNumbers)
  103. /*! @} */ // group RandomNumberFunctions