wrap_RandomGenerator.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_RandomGenerator.h"
  21. #include <cmath>
  22. namespace love
  23. {
  24. namespace math
  25. {
  26. uint64 luax_checkrandomseed(lua_State *L, int idx)
  27. {
  28. union
  29. {
  30. double seed_double;
  31. uint64 seed_uint;
  32. } s;
  33. s.seed_double = luaL_checknumber(L, idx);
  34. return s.seed_uint;
  35. }
  36. int luax_getrandom(lua_State *L, int startidx, double r)
  37. {
  38. int l, u;
  39. // from lua 5.1.4 source code: lmathlib.c:185 ff.
  40. switch (lua_gettop(L) - (startidx - 1))
  41. {
  42. case 0:
  43. lua_pushnumber(L, r);
  44. break;
  45. case 1:
  46. u = luaL_checkint(L, startidx);
  47. luaL_argcheck(L, 1 <= u, startidx, "interval is empty");
  48. lua_pushnumber(L, floor(r * u) + 1);
  49. break;
  50. case 2:
  51. l = luaL_checkint(L, startidx);
  52. u = luaL_checkint(L, startidx + 1);
  53. luaL_argcheck(L, l <= u, startidx + 1, "interval is empty");
  54. lua_pushnumber(L, floor(r * (u - l + 1)) + l);
  55. break;
  56. default:
  57. return luaL_error(L, "wrong number of arguments");
  58. }
  59. return 1;
  60. }
  61. RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
  62. {
  63. return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
  64. }
  65. int w_RandomGenerator_randomseed(lua_State *L)
  66. {
  67. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  68. uint64 seed = luax_checkrandomseed(L, 2);
  69. rng->randomseed(seed);
  70. return 0;
  71. }
  72. int w_RandomGenerator_random(lua_State *L)
  73. {
  74. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  75. return luax_getrandom(L, 2, rng->random());
  76. }
  77. int w_RandomGenerator_randnormal(lua_State *L)
  78. {
  79. RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
  80. double mean = 0.0, stddev = 1.0;
  81. if (lua_gettop(L) > 2)
  82. {
  83. mean = luaL_checknumber(L, 2);
  84. stddev = luaL_checknumber(L, 3);
  85. }
  86. else
  87. {
  88. stddev = luaL_optnumber(L, 2, 1.0);
  89. }
  90. double r = rng->randnormal(stddev);
  91. lua_pushnumber(L, r + mean);
  92. return 1;
  93. }
  94. static const luaL_Reg functions[] =
  95. {
  96. { "randomseed", w_RandomGenerator_randomseed },
  97. { "random", w_RandomGenerator_random },
  98. { "randnormal", w_RandomGenerator_randnormal },
  99. { 0, 0 }
  100. };
  101. extern "C" int luaopen_randomgenerator(lua_State *L)
  102. {
  103. return luax_register_type(L, "RandomGenerator", functions);
  104. }
  105. } // math
  106. } // love