random.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* random.cpp - random-generating nodes
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <random>
  18. #include <core/node_info.h>
  19. #include <core/node/node.h>
  20. #include <core/node/property.h>
  21. #include <core/context.h>
  22. namespace rainynite::core {
  23. namespace nodes {
  24. // TODO: use generate_canonical instead
  25. template <typename R>
  26. double get_random_in_range(R& engine, double max) {
  27. static_assert(R::min() == 0);
  28. double random = engine() * max;
  29. random /= R::max()+1.0;
  30. return random;
  31. }
  32. /**
  33. * Node that generates list of static random numbers.
  34. *
  35. * Currently it returns doubles in [0; 1) range because that's more
  36. * useful than returning random 32bit ints and strict precision isn't
  37. * that important yet.
  38. */
  39. class RandomList : public Node<vector<double>> {
  40. public:
  41. RandomList() {
  42. init<double>(length, 0);
  43. init<double>(seed, 0);
  44. init<double>(max, 1);
  45. }
  46. protected:
  47. vector<NodeInContext> get_list_links(shared_ptr<Context> ctx) const override {
  48. vector<NodeInContext> result;
  49. random_sequence(ctx, [&result, ctx](auto r) {
  50. result.emplace_back(make_value<double>(r), ctx);
  51. });
  52. return result;
  53. }
  54. public:
  55. vector<double> get(shared_ptr<Context> ctx) const override {
  56. vector<double> result;
  57. random_sequence(ctx, [&result](auto r) {
  58. result.push_back(r);
  59. });
  60. return result;
  61. }
  62. private:
  63. template <typename F>
  64. void random_sequence(shared_ptr<Context> ctx, F f) const {
  65. try {
  66. int length = get_length()->get(ctx);
  67. if (length < 1)
  68. return;
  69. auto seed = get_seed()->get(ctx);
  70. auto max = get_max()->get(ctx);
  71. random_engine.seed(seed);
  72. // TODO: cache
  73. while (length--) {
  74. auto random = get_random_in_range(random_engine, max);
  75. f(random);
  76. }
  77. } catch (...) {
  78. }
  79. }
  80. private:
  81. mutable std::mt19937 random_engine;
  82. private:
  83. NODE_PROPERTY(length, double);
  84. NODE_PROPERTY(seed, double);
  85. NODE_PROPERTY(max, double);
  86. };
  87. REGISTER_NODE(RandomList);
  88. class MovingRandom : public Node<double> {
  89. public:
  90. MovingRandom() {
  91. init<double>(max, 1);
  92. init<double>(period, 1);
  93. init<double>(seed, 0);
  94. }
  95. public:
  96. double get(shared_ptr<Context> ctx) const {
  97. try {
  98. auto max = get_max()->get(ctx);
  99. auto period = get_period()->get(ctx);
  100. auto seed = get_seed()->get(ctx);
  101. random_engine.seed(seed);
  102. double p = ctx->get_time().get_seconds() / period;
  103. random_engine.discard((int)std::floor(p));
  104. auto a = get_random_in_range(random_engine, max);
  105. auto b = get_random_in_range(random_engine, max);
  106. auto offset = p - std::floor(p);
  107. return a*(1-offset)+b*offset;
  108. } catch (...) {
  109. return 0;
  110. }
  111. }
  112. private:
  113. mutable std::mt19937 random_engine;
  114. private:
  115. NODE_PROPERTY(max, double);
  116. NODE_PROPERTY(period, double);
  117. NODE_PROPERTY(seed, double);
  118. };
  119. REGISTER_NODE(MovingRandom);
  120. } // namespace nodes
  121. } // namespace rainynite::core