Skip to content

Instantly share code, notes, and snippets.

@Sam-Belliveau
Last active September 6, 2019 22:57
Show Gist options
  • Save Sam-Belliveau/604efbd235ee9d68a887f1cc6bbfbfda to your computer and use it in GitHub Desktop.
Save Sam-Belliveau/604efbd235ee9d68a887f1cc6bbfbfda to your computer and use it in GitHub Desktop.
a c++ implementation of xoshiro256**
#ifndef XOSHIRO_256_RNG_ALGORITHM
#define XOSHIRO_256_RNG_ALGORITHM
#include <random>
#include <cstdint>
#include <limits>
namespace xoshiro256
{
class xoshiro256
{
public: // Static Members
using result_type = std::uint64_t;
static constexpr result_type default_seed = 1;
static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
public: // Public functions
xoshiro256(const result_type in_seed = default_seed) { seed(in_seed); }
xoshiro256(std::seed_seq& in_seed) { seed(in_seed); }
void seed(const result_type in_seed)
{ std::seed_seq t{in_seed}; seed(t);}
void seed(std::seed_seq& in_seed)
{ in_seed.generate(std::begin(state_), std::end(state_)); }
/*** Generating Random Numbers ***/
result_type operator()()
{
const result_type result = rotate_left(state_[1] * 5, 7) * 9;
const result_type t = state_[1] << 17;
state_[2] ^= state_[0];
state_[3] ^= state_[1];
state_[1] ^= state_[2];
state_[0] ^= state_[3];
state_[2] ^= t;
state_[3] = rotate_left(state_[3], 45);
return result;
}
void discard(std::size_t dis)
{
for(std::size_t i = 0; i < dis; ++i) operator()();
}
private: // Functions
static result_type rotate_left(result_type in, result_type r)
{
return (in << r) | (in >> (64 - r));
}
private: // Variables
result_type state_[4];
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment