rand: add the splitmix64 generator

This is the suggested initialiser for xoshiro256plusplus state.
This commit is contained in:
Danny Robson 2020-08-17 13:55:54 +10:00
parent bf3ae14f25
commit 4bd9ec982b
3 changed files with 47 additions and 0 deletions

View File

@ -487,6 +487,7 @@ list (
rand/pcg.hpp rand/pcg.hpp
rand/rdrand.cpp rand/rdrand.cpp
rand/rdrand.hpp rand/rdrand.hpp
rand/splitmix64.hpp
rand/system.hpp rand/system.hpp
random.cpp random.cpp
random.hpp random.hpp

44
rand/splitmix64.hpp Normal file
View File

@ -0,0 +1,44 @@
/* Written in 2015 by Sebastiano Vigna (vigna@acm.org)
*
* To the extent possible under law, the author has dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*/
#include "../std.hpp"
#include <limits>
namespace cruft::rand {
class splitmix64 {
public:
using result_type = u64;
splitmix64 (u64 seed):
m_state (seed)
{ ; }
result_type operator() (void)
{
u64 z = (m_state += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
static constexpr result_type min (void)
{
return std::numeric_limits<result_type>::min ();
}
static constexpr result_type max (void)
{
return std::numeric_limits<result_type>::max ();
}
private:
u64 m_state;
};
}

View File

@ -4,6 +4,7 @@
#include "rand/pcg.hpp" #include "rand/pcg.hpp"
#include "rand/rdrand.hpp" #include "rand/rdrand.hpp"
#include "rand/system.hpp" #include "rand/system.hpp"
#include "rand/splitmix64.hpp"
#include "tap.hpp" #include "tap.hpp"
#include "maths.hpp" #include "maths.hpp"
@ -49,6 +50,7 @@ main (int,char**)
{ {
cruft::TAP::logger tap; cruft::TAP::logger tap;
test_buckets<cruft::rand::splitmix64> (tap, 0x1234u);
test_buckets<cruft::rand::xorshift<uint32_t>> (tap, 0x1234u); test_buckets<cruft::rand::xorshift<uint32_t>> (tap, 0x1234u);
test_buckets<cruft::rand::xorshift<uint64_t>> (tap, 0x1234u); test_buckets<cruft::rand::xorshift<uint64_t>> (tap, 0x1234u);
test_buckets<cruft::rand::lcg_t> (tap, 0x1234u); test_buckets<cruft::rand::lcg_t> (tap, 0x1234u);