Skip to content

Seed Sequence

The goal of seed_seq is to generate reasonable seed data based on multiple entropy arguments or multiple fixed seeds. It is an alternative implementation of M. E. O'Neill's seed_seq Alternative. Take a look at her website to find out how it is working. seed_seq is a k-to-1 mapping and has good avalanche properties. In general, it provides much better behavior than std::seed_seq. We recommend to use seed_seq when possible.

Scalar

namespace pxart {

template <typename T, size_t N>
struct seed_seq;

using seed_seq_4x32 = seed_seq<uint32_t, 4>;
using seed_seq_8x32 = seed_seq<uint32_t, 8>;
using seed_seq_4x64 = seed_seq<uint64_t, 4>;

}

Include Scheme

#include <pxart/seed_seq.hpp>

Member Types and Type Variables

using uint_type = T;
using result_type = uint_type;
static constexpr size_t state_size = N;

Member Functions

Construction

constexpr seed_seq() noexcept;
Default construction of pxart::seed_seq. Calls other constructor with std::initializer_list and argument {1, 2, 3, 4}.


template <typename U>
seed_seq(std::initializer_list<U> init) noexcept;
Constructor for multiple entropy sources or multiple fixed seed values. This constructor calls the constructor based on input iterators.


template <typename InputIt>
seed_seq(InputIt begin, InputIt end) noexcept;
Constructor generates the state of the object by first hashing the input values and then mixing them by applying another hash and mixing function. If the given number of values is smaller than the actual state then the given entropy will be stretched to all state values.

Generation

constexpr result_type operator()() noexcept;
Returns a pseudorandom number based on the current state of the object. This function is used to seed other PRNGs in a reasonable way.

Example

#include <iomanip>
#include <iostream>
//
#include <pxart/mt19937.hpp>
#include <pxart/seed_seq.hpp>

using namespace std;

int main() {
  // Construct PRNG by seeding it with seed_seq_8x32 which itself uses four
  // fixed seed values.
  pxart::seed_seq_8x32 seeder{1, 2, 3, 4};
  pxart::mt19937 rng{seeder};

  // Return some pseudorandom numbers.
  for (int i = 0; i < 10; ++i) cout << setw(15) << rng() << '\n';
}

Last update: January 18, 2021