-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No setting the seed value after creation? #5
Comments
To re-seed, you can assign a new instance with the desired seed: auto rng = Xoshiro::Xoshiro256Plus( 1234 );
//... use rng ...
rng = Xoshiro::Xoshiro256Plus( 6789 );
//... use rng with the new seed ... It's unlikely that methods like You can track the seed separately, as shown in the examples; or use a wrapper class to store the seed, something like: template <class Generator>
struct Seedful : Generator
{
using seed_type = std::uint64_t;
constexpr explicit Seedful(seed_type seed = XoshiroCpp::DefaultSeed) noexcept
: Generator(seed), m_seed(seed)
{}
constexpr void setSeed(seed_type seed)
{
m_seed = seed;
this->deserialize(Generator(seed).serialize());
}
[[nodiscard]]
constexpr seed_type getSeed() const noexcept
{
return m_seed;
}
private:
seed_type m_seed;
}; Usage: auto rngA = Seedful<XoshiroCpp::Xoshiro256Plus>( 1234 );
assert(rngA.getSeed() == 1234);
for (int i : {1, 2, 3}) { std::cout << rngA() << "\n"; }
assert(rngA.getSeed() == 1234);
rngA.setSeed( 555 );
assert(rngA.getSeed() == 555);
auto rngB = XoshiroCpp::Xoshiro256Plus( 555 );
for (int i: {1, 2, 3}) { assert(rngA() == rngB()); }
Full example on godbolt. |
Very nice! :) |
Normally, yes, for expensive objects. However, in this case, assigning a new instance is the efficient way, because:
Yes, it strives to generate a uniform random distribution of unsigned numbers within the full range of bits. So a 64-bit generator would produce numbers in the range The intended usage would be similar to how you would use
This might be a little off topic for this thread, but you might find more "visually pleasing" randomness from something like https://github.com/Reputeless/PerlinNoise. |
Very interesting! Thanks for taking the time to teach me that! :) |
I'd like to be able to modify the seed number during runtime. It's a useful utility. Is this RNG good for that?
setSeed(int64_t)
getSeed() return int64_t
The text was updated successfully, but these errors were encountered: