Loading...
Searching...
No Matches
SplitMix64.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
12
13#ifndef GPR_BO_SPLITMIX64_H_
14#define GPR_BO_SPLITMIX64_H_
15
16#include <Eigen/Core>
17#include <cmath>
18#include <cstdint>
19
20namespace gpr::bo {
21
23public:
24 explicit SplitMix64(std::uint64_t seed) : state_(seed) { }
25
26 std::uint64_t next()
27 {
28 std::uint64_t value = (state_ += 0x9e3779b97f4a7c15ULL);
29 value = (value ^ (value >> 30U)) * 0xbf58476d1ce4e5b9ULL;
30 value = (value ^ (value >> 27U)) * 0x94d049bb133111ebULL;
31 return value ^ (value >> 31U);
32 }
33
35 double uniformOpen()
36 {
37 constexpr double inverse =
38 1.0 / static_cast<double>(std::uint64_t{1} << 53U);
39 return (static_cast<double>(next() >> 11U) + 0.5) * inverse;
40 }
41
42private:
43 std::uint64_t state_;
44};
45
47inline void fillStandardNormal(SplitMix64& generator, Eigen::VectorXd& normal)
48{
49 const double two_pi = 2.0 * std::acos(-1.0);
50 for (Eigen::Index index = 0; index < normal.size(); index += 2) {
51 const double radius =
52 std::sqrt(-2.0 * std::log(generator.uniformOpen()));
53 const double angle = two_pi * generator.uniformOpen();
54 normal[index] = radius * std::cos(angle);
55 if (index + 1 < normal.size()) {
56 normal[index + 1] = radius * std::sin(angle);
57 }
58 }
59}
60
61} // namespace gpr::bo
62
63#endif // GPR_BO_SPLITMIX64_H_
std::uint64_t next()
Definition SplitMix64.h:26
double uniformOpen()
Uniform on the open interval (0, 1).
Definition SplitMix64.h:35
SplitMix64(std::uint64_t seed)
Definition SplitMix64.h:24
void fillStandardNormal(SplitMix64 &generator, Eigen::VectorXd &normal)
Independent standard normals, two per pair of uniforms (Box-Muller).
Definition SplitMix64.h:47