Skip to content

Commit

Permalink
Merge pull request #89104 from luevano/fix-rand-weighted
Browse files Browse the repository at this point in the history
Fix `RandomNumberGenerator::rand_weighted` return type
  • Loading branch information
akien-mga committed Mar 4, 2024
2 parents de59a95 + 981883d commit 48aa120
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/math/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RandomNumberGenerator : public RefCounted {
_FORCE_INLINE_ real_t randfn(real_t p_mean = 0.0, real_t p_deviation = 1.0) { return randbase.randfn(p_mean, p_deviation); }
_FORCE_INLINE_ int randi_range(int p_from, int p_to) { return randbase.random(p_from, p_to); }

_FORCE_INLINE_ int rand_weighted(const Vector<float> &p_weights) { return randbase.rand_weighted(p_weights); }
_FORCE_INLINE_ int64_t rand_weighted(const Vector<float> &p_weights) { return randbase.rand_weighted(p_weights); }

RandomNumberGenerator() { randbase.randomize(); }
};
Expand Down
2 changes: 1 addition & 1 deletion core/math/random_pcg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void RandomPCG::randomize() {
seed(((uint64_t)OS::get_singleton()->get_unix_time() + OS::get_singleton()->get_ticks_usec()) * pcg.state + PCG_DEFAULT_INC_64);
}

int RandomPCG::rand_weighted(const Vector<float> &p_weights) {
int64_t RandomPCG::rand_weighted(const Vector<float> &p_weights) {
ERR_FAIL_COND_V_MSG(p_weights.is_empty(), -1, "Weights array is empty.");
int64_t weights_size = p_weights.size();
const float *weights = p_weights.ptr();
Expand Down
2 changes: 1 addition & 1 deletion core/math/random_pcg.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RandomPCG {
return pcg32_boundedrand_r(&pcg, bounds);
}

int rand_weighted(const Vector<float> &p_weights);
int64_t rand_weighted(const Vector<float> &p_weights);

// Obtaining floating point numbers in [0, 1] range with "good enough" uniformity.
// These functions sample the output of rand() as the fraction part of an infinite binary number,
Expand Down
6 changes: 3 additions & 3 deletions doc/classes/RandomNumberGenerator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
Returns a random index with non-uniform weights. Prints an error and returns [code]-1[/code] if the array is empty.
[codeblocks]
[gdscript]
var rnd = RandomNumberGenerator.new()
var rng = RandomNumberGenerator.new()

var my_array = ["one", "two", "three, "four"]
var my_array = ["one", "two", "three", "four"]
var weights = PackedFloat32Array([0.5, 1, 1, 2])

# Prints one of the four elements in `my_array`.
# It is more likely to print "four", and less likely to print "two".
# It is more likely to print "four", and less likely to print "one".
print(my_array[rng.rand_weighted(weights)])
[/gdscript]
[/codeblocks]
Expand Down

0 comments on commit 48aa120

Please sign in to comment.