rand/lcg: add naive LCG RNG

This commit is contained in:
Danny Robson 2016-02-03 12:13:03 +11:00
parent 2224131955
commit c8afad1e5b
5 changed files with 176 additions and 2 deletions

View File

@ -263,6 +263,8 @@ UTIL_FILES = \
quaternion.cpp \ quaternion.cpp \
quaternion.hpp \ quaternion.hpp \
raii.hpp \ raii.hpp \
rand/lcg.cpp \
rand/lcg.hpp \
rand/xorshift.cpp \ rand/xorshift.cpp \
rand/xorshift.hpp \ rand/xorshift.hpp \
random.cpp \ random.cpp \
@ -425,7 +427,7 @@ TEST_BIN = \
test/point \ test/point \
test/polynomial \ test/polynomial \
test/pool \ test/pool \
test/rand/xorshift \ test/rand/buckets \
test/random \ test/random \
test/range \ test/range \
test/rational \ test/rational \

60
rand/lcg.cpp Normal file
View File

@ -0,0 +1,60 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "lcg.hpp"
using util::rand::lcg;
template <typename T>
static constexpr
bool is_coprime (T M, T C)
{
if (M == 0)
return true;
if (util::gcd (M, C) == 1u)
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T, T M, T A, T C>
lcg<T,M,A,C>::lcg (T seed):
m_x (seed)
{
// ensure this assertion isn't in a header, it could be pretty expensive
// to evaluate often.
static_assert (is_coprime (M, C),
"multiplier and increment must be coprime");
}
///////////////////////////////////////////////////////////////////////////////
template <typename T, T M, T A, T C>
T
lcg<T,M,A,C>::operator() (void)
{
m_x = (A * m_x + C);
if (M != 0)
m_x %= M;
return m_x;
}
///////////////////////////////////////////////////////////////////////////////
template struct util::rand::lcg<uint32_t, util::pow2(31), 1103515245, 12345>;
template struct util::rand::lcg<uint64_t, 0ul, 6364136223846793005ul, 1ul>;

47
rand/lcg.hpp Normal file
View File

@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_RAND_LCG_HPP
#define __UTIL_RAND_LCG_HPP
#include "../maths.hpp"
namespace util { namespace rand {
/// linear congruential generator
///
/// T: output/state type
/// M: modulus
/// A: multiplier
/// C: increment
template <typename T, T M, T A, T C>
struct lcg {
public:
static_assert (std::is_unsigned<T>::value,
"LCG generates integer overflow which is undefined behaviour for signed types");
lcg (T seed);
T operator() (void);
private:
T m_x;
};
// glibc: typedef lcg<uint32_t, pow2(31), 1103515245, 12345> lcg_t;
using lcg_t = lcg<uint64_t,0u,6364136223846793005ul, 1ul>;
} }
#endif

3
test/.gitignore vendored
View File

@ -15,7 +15,8 @@
/options /options
/point /point
/pool /pool
/rand /random
/rand/buckets
/range /range
/region /region
/ripemd /ripemd

64
test/rand/buckets.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "rand/xorshift.hpp"
#include "rand/lcg.hpp"
#include "tap.hpp"
#include "maths.hpp"
#include "types/string.hpp"
///////////////////////////////////////////////////////////////////////////////
template <>
std::string
type_to_string<util::rand::xorshift<uint32_t>> (void) { return "xorshift<uint32_t>"; }
template <>
std::string
type_to_string<util::rand::xorshift<uint64_t>> (void) { return "xorshift<uint64_t>"; }
template <>
std::string
type_to_string<util::rand::lcg_t> (void) { return "lcg_t"; }
///////////////////////////////////////////////////////////////////////////////
/// check random outputs are roughly divisible between a number of fixed width
/// buckets.
///
/// this is not anything close to a strict statistical test. it is more aimed
/// at link testing and basic functionality verification within a small
/// resource footprint (ie, fast unit testing).
template <typename G, typename ...Args>
void
test_buckets (util::TAP::logger &tap, Args&& ...args)
{
constexpr unsigned BUCKET_BITS = 8u;
constexpr size_t BUCKET_COUNT = 1u << BUCKET_BITS;
constexpr unsigned BUCKET_MASK = BUCKET_COUNT - 1u;
constexpr unsigned EXPECTED = 1024u;
constexpr unsigned ITERATIONS = BUCKET_COUNT * EXPECTED;
unsigned buckets[BUCKET_COUNT] = {};
G gen (std::forward<Args> (args)...);
for (unsigned i = 0; i < ITERATIONS; ++i)
++buckets[gen () & BUCKET_MASK];
tap.expect (std::find_if (std::cbegin (buckets),
std::cend (buckets),
[] (auto v) { return v < EXPECTED * 3 / 4; }) == std::cend (buckets),
"bucket counts for %s", type_to_string<G> ());
}
///////////////////////////////////////////////////////////////////////////////
int
main (int,char**)
{
util::TAP::logger tap;
test_buckets<util::rand::xorshift<uint32_t>> (tap, 0x1234u);
test_buckets<util::rand::xorshift<uint64_t>> (tap, 0x1234u);
test_buckets<util::rand::lcg_t> (tap, 0x1234u);
return tap.status ();
}