random.cpp 3.7 KB

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