RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
seeding.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <chrono>
8#include <concepts>
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <random>
13#include <thread>
14#include <vector>
15
16namespace randlib
17{
18
21
26inline std::uint64_t random_seed()
27{
28 std::uint64_t entropy = 0;
29 try {
30 std::random_device device;
31 entropy = (static_cast<std::uint64_t>(device()) << 32) ^ device();
32 } catch (const std::exception &) {
33 }
34 entropy ^= static_cast<std::uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
35 entropy ^= static_cast<std::uint64_t>(std::hash<std::thread::id>()(std::this_thread::get_id())) << 1;
36 return SplitMix64(entropy)();
37}
38
43template <class Engine = DefaultEngine>
44Engine seed_from(std::seed_seq &sequence)
45{
46 return Engine(sequence);
47}
48
56template <class Engine = DefaultEngine>
57std::vector<Engine> make_streams(std::uint64_t seed, std::size_t count)
58{
59 std::vector<Engine> streams;
60 streams.reserve(count);
61 if constexpr (requires(Engine e) { e.jump(); }) {
62 Engine engine(seed);
63 for (std::size_t i = 0; i < count; ++i) {
64 streams.push_back(engine);
65 engine.jump();
66 }
67 } else if constexpr (std::constructible_from<Engine, std::uint64_t, std::uint64_t>) {
68 for (std::size_t i = 0; i < count; ++i)
69 streams.emplace_back(seed, static_cast<std::uint64_t>(i));
70 } else {
71 SplitMix64 seeds(seed);
72 for (std::size_t i = 0; i < count; ++i)
73 streams.emplace_back(static_cast<typename Engine::result_type>(seeds()));
74 }
75 return streams;
76}
77
78} // namespace randlib
The SplitMix64 class Sebastiano Vigna's SplitMix64 generator.
Definition splitmix64.hpp:14
The Xoshiro256pp class xoshiro256++ 1.0 by David Blackman and Sebastiano Vigna: 256 bits of state,...
Definition xoshiro.hpp:20
Definition bivariate_normal.hpp:10
std::vector< Engine > make_streams(std::uint64_t seed, std::size_t count)
Engines for parallel simulations, one per thread: consecutive jump() calls for engines that have them...
Definition seeding.hpp:57
std::uint64_t random_seed()
Definition seeding.hpp:26
Engine seed_from(std::seed_seq &sequence)
Definition seeding.hpp:44