Skip to content

Commit

Permalink
Add ability to restore RandomNumberGenerator state
Browse files Browse the repository at this point in the history
- added `state` as a property to restore internal state of RNG;
- `get_seed()` returns last seed used to initialize the state rather than the current state.

Co-authored-by: MidZik <matt.idzik1@gmail.com>
  • Loading branch information
Andrii Doroshenko (Xrayez) and MidZik committed Dec 7, 2020
1 parent 3dc8aaa commit b510771
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
7 changes: 6 additions & 1 deletion core/math/random_number_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void RandomNumberGenerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_seed", "seed"), &RandomNumberGenerator::set_seed);
ClassDB::bind_method(D_METHOD("get_seed"), &RandomNumberGenerator::get_seed);

ClassDB::bind_method(D_METHOD("set_state", "state"), &RandomNumberGenerator::set_state);
ClassDB::bind_method(D_METHOD("get_state"), &RandomNumberGenerator::get_state);

ClassDB::bind_method(D_METHOD("randi"), &RandomNumberGenerator::randi);
ClassDB::bind_method(D_METHOD("randf"), &RandomNumberGenerator::randf);
ClassDB::bind_method(D_METHOD("randfn", "mean", "deviation"), &RandomNumberGenerator::randfn, DEFVAL(0.0), DEFVAL(1.0));
Expand All @@ -42,6 +45,8 @@ void RandomNumberGenerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("randomize"), &RandomNumberGenerator::randomize);

ADD_PROPERTY(PropertyInfo(Variant::INT, "seed"), "set_seed", "get_seed");
// Default value is non-deterministic, override it for doc generation purposes.
ADD_PROPERTY(PropertyInfo(Variant::INT, "state"), "set_state", "get_state");
// Default values are non-deterministic, override for doc generation purposes.
ADD_PROPERTY_DEFAULT("seed", 0);
ADD_PROPERTY_DEFAULT("state", 0);
}
8 changes: 3 additions & 5 deletions core/math/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ class RandomNumberGenerator : public Reference {

public:
_FORCE_INLINE_ void set_seed(uint64_t p_seed) { randbase.seed(p_seed); }

_FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); }

_FORCE_INLINE_ void set_state(uint64_t p_state) { randbase.set_state(p_state); }
_FORCE_INLINE_ uint64_t get_state() const { return randbase.get_state(); }

_FORCE_INLINE_ void randomize() { randbase.randomize(); }

_FORCE_INLINE_ uint32_t randi() { return randbase.rand(); }

_FORCE_INLINE_ real_t randf() { return randbase.randf(); }

_FORCE_INLINE_ real_t randf_range(real_t p_from, real_t p_to) { return randbase.random(p_from, p_to); }

_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); }

RandomNumberGenerator() {}
Expand Down
7 changes: 4 additions & 3 deletions core/math/random_pcg.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static int __bsr_clz32(uint32_t x) {

class RandomPCG {
pcg32_random_t pcg;
uint64_t current_seed; // seed with this to get the same state
uint64_t current_seed; // The seed the current generator state started from.
uint64_t current_inc;

public:
Expand All @@ -76,13 +76,14 @@ class RandomPCG {
}
_FORCE_INLINE_ uint64_t get_seed() { return current_seed; }

_FORCE_INLINE_ void set_state(uint64_t p_state) { pcg.state = p_state; }
_FORCE_INLINE_ uint64_t get_state() const { return pcg.state; }

void randomize();
_FORCE_INLINE_ uint32_t rand() {
current_seed = pcg.state;
return pcg32_random_r(&pcg);
}
_FORCE_INLINE_ uint32_t rand(uint32_t bounds) {
current_seed = pcg.state;
return pcg32_boundedrand_r(&pcg, bounds);
}

Expand Down
22 changes: 20 additions & 2 deletions doc/classes/RandomNumberGenerator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
rng.randomize()
var my_random_number = rng.randf_range(-10.0, 10.0)
[/codeblock]
[b]Note:[/b] The default values of [member seed] and [member state] properties are pseudo-random, and changes when calling [method randomize]. The [code]0[/code] value documented here is a placeholder, and not the actual default seed.
</description>
<tutorials>
<link title="Random number generation">https://docs.godotengine.org/en/latest/tutorials/math/random_number_generation.html</link>
Expand Down Expand Up @@ -75,9 +76,26 @@
</methods>
<members>
<member name="seed" type="int" setter="set_seed" getter="get_seed" default="0">
The seed used by the random number generator. A given seed will give a reproducible sequence of pseudo-random numbers.
Initializes the random number generator state based on the given seed value. A given seed will give a reproducible sequence of pseudo-random numbers.
[b]Note:[/b] The RNG does not have an avalanche effect, and can output similar random streams given similar seeds. Consider using a hash function to improve your seed quality if they're sourced externally.
[b]Note:[/b] The default value of this property is pseudo-random, and changes when calling [method randomize]. The [code]0[/code] value documented here is a placeholder, and not the actual default seed.
[b]Note:[/b] Setting this property produces a side effect of changing the internal [member state], so make sure to initialize the seed [i]before[/i] modifying the [member state]:
[codeblock]
var rng = RandomNumberGenerator.new()
rng.seed = hash("Godot")
rng.state = 100 # Restore to some previously saved state.
[/codeblock]
</member>
<member name="state" type="int" setter="set_state" getter="get_state" default="0">
The current state of the random number generator. Save and restore this property to restore the generator to a previous state:
[codeblock]
var rng = RandomNumberGenerator.new()
print(rng.randf())
var saved_state = rng.state # Store current state.
print(rng.randf()) # Advance internal state.
rng.state = saved_state # Restore the state.
print(rng.randf()) # Prints the same value as in previous.
[/codeblock]
[b]Note:[/b] Do not set state to arbitrary values, since the random number generator requires the state to have certain qualities to behave properly. It should only be set to values that came from the state property itself. To initialize the random number generator with arbitrary input, use [member seed] instead.
</member>
</members>
<constants>
Expand Down

0 comments on commit b510771

Please sign in to comment.