Skip to content

Instantly share code, notes, and snippets.

@tgockel
Created August 18, 2016 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgockel/96eb9e05e6db616d5075b99fca5476bc to your computer and use it in GitHub Desktop.
Save tgockel/96eb9e05e6db616d5075b99fca5476bc to your computer and use it in GitHub Desktop.
#include "random_seed.hpp"
int main()
{
// Just test that this compiles...not really sure what to do with these things
tgl::random_device_seed seed;
std::mt19937_64 mt_rng(seed);
std::minstd_rand lc_rng(seed);
std::ranlux48 lx_rng(seed);
}
/** Copyright 2016 Travis Gockel.
*
* 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.
**/
#pragma once
#include <cstdint>
#include <initializer_list>
#include <random>
namespace tgl
{
/** A [seed sequence](http://en.cppreference.com/w/cpp/concept/SeedSequence) partial implementation whose generate
* function pulls data from an RNG. It is a partial implementation in that it does not support saving the current state
* to generate the same sequence later (use \c std::seed_seq for this).
*
* \tparam TRandomSource The source of values for this seed sequence generator.
* \tparam TDistribution A [random number distribution](http://en.cppreference.com/w/cpp/concept/RandomNumberDistribution)
**/
template <typename TRandomSource,
typename TDistribution = std::uniform_int_distribution<std::uint32_t>
>
class random_seed
{
public:
using random_source_type = TRandomSource;
using distribution_type = TDistribution;
using distribution_param_type = typename distribution_type::param_type;
using result_type = typename distribution_type::result_type;
public:
random_seed() :
rng(),
dist()
{ }
template <typename TSeed>
explicit random_seed(TSeed seed, distribution_param_type dist_param = distribution_param_type()) :
rng(seed),
dist(dist_param)
{ }
template <typename TInputIterator>
explicit random_seed(TInputIterator first,
TInputIterator last,
distribution_param_type dist_param = distribution_param_type()
) :
rng(first, last),
dist(dist_param)
{ }
template <typename T>
explicit random_seed(std::initializer_list<T> seed,
distribution_param_type dist_param = distribution_param_type()
) :
random_seed(seed.begin(), seed.end(), dist_param)
{ }
/** Fill `[first, last)` with values from the source distribution. **/
template <typename TOutputIterator>
void generate(TOutputIterator first, TOutputIterator last)
{
for ( ; first != last; ++first)
*first = dist(rng);
}
/** Generate a single value from the distribution. **/
result_type operator()()
{
return dist(rng);
}
private:
random_source_type rng;
distribution_type dist;
};
using random_device_seed = random_seed<std::random_device>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment