random_pcg.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**************************************************************************/
  2. /* random_pcg.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "random_pcg.h"
  31. #include "core/os/os.h"
  32. #include "core/templates/vector.h"
  33. RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
  34. pcg(),
  35. current_inc(p_inc) {
  36. seed(p_seed);
  37. }
  38. void RandomPCG::randomize() {
  39. seed(((uint64_t)OS::get_singleton()->get_unix_time() + OS::get_singleton()->get_ticks_usec()) * pcg.state + PCG_DEFAULT_INC_64);
  40. }
  41. int64_t RandomPCG::rand_weighted(const Vector<float> &p_weights) {
  42. ERR_FAIL_COND_V_MSG(p_weights.is_empty(), -1, "Weights array is empty.");
  43. int64_t weights_size = p_weights.size();
  44. const float *weights = p_weights.ptr();
  45. float weights_sum = 0.0;
  46. for (int64_t i = 0; i < weights_size; ++i) {
  47. weights_sum += weights[i];
  48. }
  49. float remaining_distance = randf() * weights_sum;
  50. for (int64_t i = 0; i < weights_size; ++i) {
  51. remaining_distance -= weights[i];
  52. if (remaining_distance < 0) {
  53. return i;
  54. }
  55. }
  56. for (int64_t i = weights_size - 1; i >= 0; --i) {
  57. if (weights[i] > 0) {
  58. return i;
  59. }
  60. }
  61. return -1;
  62. }
  63. double RandomPCG::random(double p_from, double p_to) {
  64. return randd() * (p_to - p_from) + p_from;
  65. }
  66. float RandomPCG::random(float p_from, float p_to) {
  67. return randf() * (p_to - p_from) + p_from;
  68. }
  69. int RandomPCG::random(int p_from, int p_to) {
  70. if (p_from == p_to) {
  71. return p_from;
  72. }
  73. return int(rand(uint32_t(Math::abs(p_from - p_to)) + 1U)) + MIN(p_from, p_to);
  74. }