https://github.com/avitase/stateless_rand

Stateless and pure functional (pseudo) C++ random generator

https://github.com/avitase/stateless_rand

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.8%) to scientific vocabulary

Keywords

constexpr cpp header-only pure-functional random-number-generators stateless
Last synced: 5 months ago · JSON representation

Repository

Stateless and pure functional (pseudo) C++ random generator

Basic Info
  • Host: GitHub
  • Owner: avitase
  • Language: C++
  • Default Branch: master
  • Homepage:
  • Size: 125 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
constexpr cpp header-only pure-functional random-number-generators stateless
Created almost 7 years ago · Last pushed over 6 years ago
Metadata Files
Readme

README.md

stateless_rand

stateless_rand is a header-only (include/statelessrnd.hpp), stateless (pseudo) C++ random generator / number that implements the linear congruential relation x <- (x * a + c) % m, where x is the current state value, and a, c and m are referred to as multiplier, increment and modulus parameter, respectively. As all linear congruential algorithms, the generated (pseudo) random values suffer from the Marsaglia effect but benefit from the small state size and blazingly fast calculation. Always keep this in mind and use it carefully! (Especially, this library should not be used for security relevant aspects of your project!)

In marked difference to the implementation available within the STL, this implementation is purely functional and thus can be used in constexpr contexts without sacrificing functionality. The most obvious differences to STL's std::linear_congruential_engine are the initialization via the static seed() method and the next() member function, which, instead of altering the internal state, returns a new instance of stateless_rand that represents the next value in the (pseudo) random sequence. Since the internal (constant) state has the size of an integer, this operation is efficient. It is impossible to change the internal state of the generator, it could hence also be considered a random number itself than a generator. using namespace statelessrnd; constexpr auto rnd = minstd_rand::seed(42u); // 2027382 constexpr auto rnd2 = rnd.next(); // 1226992407

The state can be accessed via three equivalent methods: * (dereferencing operator), value() or the conversion operator to the underlying type. constexpr auto a = *rnd; // 2027382 constexpr auto b = rnd.value(); // 2027382 constexpr decltype(rnd)::value_type c{rnd}; // 2027382 static_assert(a == b && a == c);

The sequence is seeded with a seed seed via seed(seed). The first value (seed) is skipped by default, this can be changed though via setting skip_first to false. Linear congruential algorithms produce fully deterministic sequences, seeding can therefore also be used as an entry point to a given sequence. ``` constexpr auto seed = minstdrand::seed(42u, false); staticassert(*seed == 42u);

constexpr auto rnd3 = seed.next().next().next(); staticassert(minstdrand::seed(*rnd3).value() == rnd3.next().value()); ```

For the sake of compliance with std::linear_congruential_engine, we further provide getters for the minimum and maximum value of the sequence (min() and max(), respectively), as well as a mechanism to skip n values in the sequence via discard(n). The latter is equivalent to n subsequent calls of next(). Note however, that the same might also be achieved by specifying a desired value of the sequence explicitly as a seed, which can be much faster, potentially. ``` auto stlrnd = std::minstd_rand(*seed); stlrnd.discard(100); std::cout << stlrnd() << '\n'; // 544861123 std::cout << seed.discard(101).value() << '\n'; // 544861123

std::cout << minstd_rand::seed(stlrnd()).value() << '\n'; // 1571445537 std::cout << stlrnd() << '\n'; // 1571445537 ```

Seeds lower than min() or greater than max() are considered ill-formed and are clamped onto the interval [min(), max()] during during initialization. In particular, this means for any generator with c=0, that seed = 0 and seed = 1 are equivalent and produce the same sequence! static_assert(minstd_rand::seed(0u).value() == minstd_rand::seed(1u).value());

The type of the state is accessible via stateless_rand::value_type, a, c and m are template parameters. A decent choice which matches the one of std::minstd_rand, is named minstd_rand in the statlessrnd namespace of this library. using minstd_rand = stateless_rand<48271u, 0u, 2147483647u>;

Owner

  • Name: Nis Meinert
  • Login: avitase
  • Kind: user
  • Company: Pasteur Labs

GitHub Events

Total
Last Year

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 30
  • Total Committers: 1
  • Avg Commits per committer: 30.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Nis Meinert m****l@n****e 30
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels