Debian Codegraph @

core/math/random_pcg.cpp

raw symbols SCIP
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
31 #include "random_pcg.h"
32
33 #include "core/os/os.h"
34 #include "core/templates/vector.h"
35
36 RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
37 pcg(),
38 current_inc(p_inc) {
39 seed(p_seed);
40 }
41
42 void RandomPCG::randomize() {
43 seed(((uint64_t)OS::get_singleton()->get_unix_time() + OS::get_singleton()->get_ticks_usec()) * pcg.state + PCG_DEFAULT_INC_64);
44 }
45
46 int64_t RandomPCG::rand_weighted(const Vector<float> &p_weights) {
47 ERR_FAIL_COND_V_MSG(p_weights.is_empty(), -1, "Weights array is empty.");
48 int64_t weights_size = p_weights.size();
49 const float *weights = p_weights.ptr();
50 float weights_sum = 0.0;
51 for (int64_t i = 0; i < weights_size; ++i) {
52 weights_sum += weights[i];
53 }
54
55 float remaining_distance = randf() * weights_sum;
56 for (int64_t i = 0; i < weights_size; ++i) {
57 remaining_distance -= weights[i];
58 if (remaining_distance < 0) {
59 return i;
60 }
61 }
62
63 for (int64_t i = weights_size - 1; i >= 0; --i) {
64 if (weights[i] > 0) {
65 return i;
66 }
67 }
68 return -1;
69 }
70
71 double RandomPCG::random(double p_from, double p_to) {
72 return randd() * (p_to - p_from) + p_from;
73 }
74
75 float RandomPCG::random(float p_from, float p_to) {
76 return randf() * (p_to - p_from) + p_from;
77 }
78
79 int RandomPCG::random(int p_from, int p_to) {
80 if (p_from == p_to) {
81 return p_from;
82 }
83
84 int64_t min = MIN(p_from, p_to);
85 int64_t max = MAX(p_from, p_to);
86 uint32_t diff = static_cast<uint32_t>(max - min);
87
88 if (diff == UINT32_MAX) {
89 // Can't add 1 to max uint32_t value for inclusive range, so call rand without passing bounds.
90 return static_cast<int64_t>(rand()) + min;
91 }
92
93 return static_cast<int64_t>(rand(diff + 1U)) + min;
94 }