parent
160c736289
commit
acd07b6346
@ -1,6 +1,17 @@
|
||||
#ifndef _RNG_H
|
||||
#define _RNG_H
|
||||
|
||||
// C interface to C++ RNG impls
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Tests
|
||||
void frng_test();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
// RNG interfaces
|
||||
#include <rng/frng.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* _RNG_H */
|
||||
|
@ -1,17 +1,62 @@
|
||||
|
||||
#include "impl.hpp"
|
||||
#include <cmath>
|
||||
|
||||
namespace rng
|
||||
{
|
||||
struct frng : public RNG
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
inline frng(double s1, double s2) : state({s1, s2}){}
|
||||
inline frng(const std::array<double, 2>& ar) : state(ar){}
|
||||
inline frng(std::array<double, 2>&& ar) : state(ar){}
|
||||
inline frng(const double (&ar)[2]) : state({ar[0], ar[1]}) {}
|
||||
|
||||
inline constexpr double next_double() override { return sample(); }
|
||||
inline constexpr float next_float() override { return (float)sample(); }
|
||||
protected:
|
||||
double sample();
|
||||
inline constexpr double sample() override
|
||||
{
|
||||
double res = sample_double(state);
|
||||
update_state(state, res);
|
||||
return res;
|
||||
}
|
||||
private:
|
||||
std::array<double, 2> state;
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,48 +1,26 @@
|
||||
#include <rng/impl.hpp>
|
||||
#include <rng/frng.hpp>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
template<std::size_t N>
|
||||
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++)
|
||||
namespace rng {
|
||||
using namespace std;
|
||||
inline void test()
|
||||
{
|
||||
res += v[i] * u[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
frng rng(1.0, 2.0);
|
||||
|
||||
inline constexpr double fract(double x)
|
||||
{
|
||||
return x - floor(x);
|
||||
}
|
||||
for(int i=0;i<10;i++) {
|
||||
double d = rng.next_double();
|
||||
long l = rng.next_long(0, 100);
|
||||
|
||||
namespace rng {
|
||||
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);
|
||||
cout << "Sampled: " << d << endl;
|
||||
cout << "Long: " << l << endl;
|
||||
}
|
||||
}
|
||||
inline 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);
|
||||
}
|
||||
double frng::sample()
|
||||
extern "C" void frng_test()
|
||||
{
|
||||
double res = sample_double(state);
|
||||
update_state(state, res);
|
||||
return res;
|
||||
}
|
||||
rng::test();
|
||||
}
|
||||
|
Loading…
Reference in new issue