35 explicit RandomStream(std::uint64_t seed = 0, std::uint64_t stream = 0)
40 void Reseed(std::uint64_t seed, std::uint64_t stream = 0)
42 std::seed_seq sequence{
43 static_cast<std::uint32_t
>(seed),
static_cast<std::uint32_t
>(seed >> 32),
44 static_cast<std::uint32_t
>(stream),
static_cast<std::uint32_t
>(stream >> 32)};
52 return static_cast<double>(
engine_() >> 11) * 0x1.0p-53;
58 return static_cast<double>((
engine_() >> 11) + 1) * 0x1.0p-53;
70 const double angle = 6.283185307179586476925 *
Uniform();
73 return radius * std::cos(angle);
77 double Normal(
double mean,
double stdev)
83 std::uint64_t
Index(std::uint64_t n)
85 return static_cast<std::uint64_t
>(
Uniform() *
static_cast<double>(n)) % n;
102 std::random_device device;
103 return static_cast<std::uint64_t
>(device());
114 std::size_t begin = text.find_first_not_of(
" \t");
115 if (begin == std::string::npos)
120 std::size_t end = text.find_last_not_of(
" \t");
121 const std::string digits = text.substr(begin, end - begin + 1);
122 if (digits.find_first_not_of(
"0123456789") != std::string::npos || digits.size() > 20)
126 std::size_t used = 0;
127 const unsigned long long value = std::stoull(digits, &used, 10);
128 if (used != digits.size())
130 seed =
static_cast<std::uint64_t
>(value);
133 catch (
const std::exception&)
A seeded, independent stream of random numbers.
double Normal(double mean, double stdev)
Normal with mean mean and standard deviation stdev.
RandomStream(std::uint64_t seed=0, std::uint64_t stream=0)
Stream number stream derived from seed.
double UniformPositive()
Uniform on (0, 1], never zero, for taking logarithms.
double Uniform()
Uniform on [0, 1), with 53 random bits.
std::uint64_t Index(std::uint64_t n)
Uniform integer on [0, n), n > 0.
void Reseed(std::uint64_t seed, std::uint64_t stream=0)
double StandardNormal()
Standard normal, by the Box-Muller transform.
bool ParseRandomSeed(const std::string &text, std::uint64_t &seed)
Parses a user-entered seed.
constexpr std::uint64_t DefaultRandomSeed
The stream for code outside the MCMC that needs random numbers.
RandomStream & ThreadRandomStream()
std::uint64_t DrawRandomSeed()
A seed drawn from the operating system's entropy source.