Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <random>
- #include <cinttypes>
- #include <iostream>
- // This is C99's suggested RNG. It's a very basic LCG. It's fast.
- typedef std::linear_congruential_engine<uint32_t, 1103515245u, 12345u, 0u>
- c99_engine;
- volatile uint32_t forced_store;
- // An alternative to uniform_int_distribution, as found in pcg_extras;
- // assumes that the range of the RNG is strictly larger than the upper bound.
- template <typename RngType>
- auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
- -> typename RngType::result_type
- {
- typedef typename RngType::result_type rtype;
- rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
- % upper_bound;
- for (;;) {
- rtype r = rng() - RngType::min();
- if (r >= threshold)
- return r % upper_bound;
- }
- }
- int main()
- {
- c99_engine engine{0};
- std::uniform_int_distribution<> dist(0, 5);
- typedef decltype(dist.param()) params;
- for (uint32_t i = 2; i < 1000000000; ++i) {
- // forced_store = engine();
- // forced_store = dist(engine);
- // forced_store = bounded_rand(engine, 6);
- forced_store = dist(engine, params(0,i-1));
- // forced_store = bounded_rand(engine, i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement