rand: prefer our own generators

This commit is contained in:
Danny Robson 2020-08-18 07:20:26 +10:00
parent 5f4c95aa11
commit 34270e5b8f
6 changed files with 32 additions and 7 deletions

View File

@ -478,6 +478,7 @@ list (
"${CMAKE_CURRENT_BINARY_DIR}/prefix/${PREFIX}/preprocessor.hpp" "${CMAKE_CURRENT_BINARY_DIR}/prefix/${PREFIX}/preprocessor.hpp"
quaternion.cpp quaternion.cpp
quaternion.hpp quaternion.hpp
rand/generic.hpp
rand/lcg.cpp rand/lcg.cpp
rand/lcg.hpp rand/lcg.hpp
rand/mwc64x.cpp rand/mwc64x.cpp

18
rand/generic.hpp Normal file
View File

@ -0,0 +1,18 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2020, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "xoshiro.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace cruft::rand {
/// A generator with a good tradeoff between speed, size, and quality.
using general_generator = xoshiro256plusplus;
}

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include "coord/traits.hpp" #include "coord/traits.hpp"
#include "rand/generic.hpp"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@ -54,7 +55,7 @@ namespace cruft::random {
inline auto& inline auto&
generator (void) generator (void)
{ {
using generator_t = std::mt19937_64; using generator_t = cruft::rand::general_generator;
static thread_local auto gen = initialise<generator_t> (); static thread_local auto gen = initialise<generator_t> ();
return gen; return gen;
} }

View File

@ -1,7 +1,8 @@
#include "geom/sample/edge.hpp"
#include "tap.hpp"
#include "coord/iostream.hpp"
#include "coord/comparator.hpp" #include "coord/comparator.hpp"
#include "coord/iostream.hpp"
#include "geom/sample/edge.hpp"
#include "rand/generic.hpp"
#include "tap.hpp"
#include <map> #include <map>
@ -14,7 +15,7 @@ int main ()
std::map<cruft::point2i, int, cruft::coord::ordering<>> counts; std::map<cruft::point2i, int, cruft::coord::ordering<>> counts;
auto sampler = cruft::geom::sample::edge (AREA); auto sampler = cruft::geom::sample::edge (AREA);
std::mt19937_64 gen; cruft::rand::general_generator gen (42);
cruft::TAP::logger tap; cruft::TAP::logger tap;

View File

@ -8,6 +8,7 @@
#include "list/sort.hpp" #include "list/sort.hpp"
#include "random.hpp" #include "random.hpp"
#include "rand/generic.hpp"
#include "tap.hpp" #include "tap.hpp"
#include "iterator/zip.hpp" #include "iterator/zip.hpp"
@ -72,7 +73,7 @@ int main ()
std::shuffle ( std::shuffle (
std::begin (data), std::begin (data),
std::end (data), std::end (data),
std::mt19937 {COUNT} cruft::rand::general_generator {COUNT}
); );
test_sort (tap, std::move (data), "sorting large unique and shuffled array"); test_sort (tap, std::move (data), "sorting large unique and shuffled array");

View File

@ -2,6 +2,7 @@
#include "functor.hpp" #include "functor.hpp"
#include "geom/aabb.hpp" #include "geom/aabb.hpp"
#include "geom/sample/surface.hpp" #include "geom/sample/surface.hpp"
#include "rand/generic.hpp"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
@ -24,10 +25,12 @@ main (int argc, char **argv)
opts.scan (argc, argv); opts.scan (argc, argv);
auto gen = cruft::random::initialise<cruft::rand::general_generator> ();
std::cout << "<svg height='" << area.h << "' width='" << area.h << "'>"; std::cout << "<svg height='" << area.h << "' width='" << area.h << "'>";
namespace sample = cruft::geom::sample; namespace sample = cruft::geom::sample;
for (auto const &p: sample::poisson (sample::surface {area}, for (auto const &p: sample::poisson (sample::surface {area},
std::default_random_engine (), gen,
cruft::functor::constant (distance), cruft::functor::constant (distance),
samples)) samples))
{ {