You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
4 years ago
|
|
||
|
#include "impl.hpp"
|
||
4 years ago
|
#include <cmath>
|
||
4 years ago
|
|
||
4 years ago
|
#include <debug.h>
|
||
|
|
||
4 years ago
|
namespace rng
|
||
|
{
|
||
|
struct frng : public RNG
|
||
|
{
|
||
4 years ago
|
template<std::size_t N>
|
||
|
static constexpr inline double dot(const std::array<double, N>& v, const std::array<double, N>& u)
|
||
|
{
|
||
|
double res=0;
|
||
|
for(std::size_t i=0;i<N;i++)
|
||
|
{
|
||
|
res += v[i] * u[i];
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
static inline constexpr double fract(double x)
|
||
|
{
|
||
|
return x - floor(x);
|
||
|
}
|
||
|
static inline constexpr double sample_double(const std::array<double, 2>& state)
|
||
|
{
|
||
|
const constexpr std::array<double, 2> vec2 = { 12.9898, 78.223 };
|
||
|
return fract(sin(dot(state, vec2)) * 43758.5453);
|
||
|
}
|
||
|
|
||
4 years ago
|
#define P D_dprintf("frng: seeded with (%f, %f)", state[0], state[1]);
|
||
|
inline constexpr frng(double s1, double s2) : state({s1, s2}){P}
|
||
|
inline constexpr frng(const std::array<double, 2>& ar) : state(ar){P}
|
||
|
inline constexpr frng(std::array<double, 2>&& ar) : state(ar){P}
|
||
|
inline constexpr frng(const double (&ar)[2]) : state({ar[0], ar[1]}) {P}
|
||
|
#undef P
|
||
4 years ago
|
inline constexpr double next_double() override { return sample(); }
|
||
|
inline constexpr float next_float() override { return (float)sample(); }
|
||
4 years ago
|
protected:
|
||
4 years ago
|
inline constexpr double sample() override
|
||
|
{
|
||
|
double res = sample_double(state);
|
||
|
update_state(state, res);
|
||
|
return res;
|
||
|
}
|
||
4 years ago
|
private:
|
||
|
std::array<double, 2> state;
|
||
4 years ago
|
static inline constexpr void update_state(std::array<double, 2>& state, double r)
|
||
|
{
|
||
|
float v1 = (float)state[0];
|
||
|
float v2 = (float)state[1];
|
||
|
|
||
|
std::array<double, 2> nvec = {
|
||
|
r,
|
||
|
(double)v2,
|
||
|
};
|
||
|
|
||
|
state[0] = sample_double(nvec);
|
||
|
|
||
|
nvec[1] = (double)v1;
|
||
|
state[1] = sample_double(nvec);
|
||
|
}
|
||
4 years ago
|
};
|
||
|
}
|
||
4 years ago
|
|