Seeding¶
We can initialise the random number state with any state that is not zero everywhere. Do do this, we take an integer and pass it through the splitmix64
algorithm until we have enough random state; this depends on the generator involved; a xoshiro512 generator requires more starting seed than a xoshiro128 generator.
These seedings functions are designed so that any single integer may be passed into them. The first form returns an initialised state:
-
template<typename T>
__host__ T dust::random::seed(uint64_t seed)¶ - Template Parameters:
T – The generator type
- Parameters:
seed – integer seed
- Returns:
A generator state (type
T
)
The second initialises a state in-place:
-
template<typename T>
__host__ void dust::random::seed(T &state, uint64_t seed)¶ - Template Parameters:
T – The generator type
- Parameters:
state – A generator state to write to
seed – integer seed; any value is suitable as it will be passed though
splitmix64
Example¶
#include <iostream>
#include <dust/random/random.hpp>
template <typename T>
void show_state(T& state, const char * label) {
std::cout << label << std::endl;
std::cout << std::hex;
for (size_t i = 0; i < T::size(); ++i) {
std::cout << state[i] << std::endl;
}
}
template <typename T>
void draw_numbers(T& state, int n, const char * label) {
std::cout << label << std::endl;
for (int i = 0; i < n; ++i) {
std::cout << dust::random::random_normal<double>(state) << std::endl;
}
}
int main() {
using rng_state_type = dust::random::generator<double>;
// construct a state as a value
auto state = dust::random::seed<rng_state_type>(1);
show_state(state, "Initial state");
draw_numbers(state, 5, "\nRandom samples");
// construct a state in place
dust::random::seed(state, 2);
show_state(state, "\nState with slightly different seed");
draw_numbers(state, 5, "\nRandom samples");
}
Initial state
910a2dec89025cc1
5e41ab087439611e
b18a02f46d8d86c3
e28195ddd9ee4956
Random samples
1.1291
0.537807
-1.26699
0.958293
3.21195
State with slightly different seed
975835de1c9756ce
64684c4f0fd784b4
1956ecd1a275ec95
90a5cb3b55b40acf
Random samples
0.965992
0.881319
-0.716755
0.551913
0.180611