/usr/include/range/v3/utility/random.hpp is in librange-v3-dev 0.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | /// \file
// Range v3 library
//
// Copyright Casey Carter 2016
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
/*
* Random-Number Utilities (randutil)
* Addresses common issues with C++11 random number generation.
* Makes good seeding easier, and makes using RNGs easy while retaining
* all the power.
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Melissa E. O'Neill
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef RANGES_V3_UTILITY_RANDOM_HPP
#define RANGES_V3_UTILITY_RANDOM_HPP
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <array>
#include <atomic>
#include <chrono>
#include <initializer_list>
#include <mutex>
#include <new>
#include <random>
#include <typeinfo>
#include <utility>
#include <meta/meta.hpp>
#include <range/v3/range_fwd.hpp>
#include <range/v3/algorithm/copy.hpp>
#include <range/v3/algorithm/generate.hpp>
#include <range/v3/utility/concepts.hpp>
#include <range/v3/utility/functional.hpp>
#include <range/v3/utility/iterator_concepts.hpp>
#if RANGES_CXX_THREAD >= RANGES_CXX_THREAD_11
#include <thread>
#endif
// Ugly platform-specific code for auto_seeded
// Clang/C2 bug: __has_builtin(__builtin_readcyclecounter) reports true, but
// there is no corresponding builtin in C2.
#if !(defined(__clang__) && defined(__c2__))
#ifdef __has_builtin
#if __has_builtin(__builtin_readcyclecounter)
#define RANGES_CPU_ENTROPY __builtin_readcyclecounter()
#endif
#endif
#endif
#ifndef RANGES_CPU_ENTROPY
#ifdef __i386__
#ifdef __GNUC__
#define RANGES_CPU_ENTROPY __builtin_ia32_rdtsc()
#else
#include <immintrin.h>
#define RANGES_CPU_ENTROPY __rdtsc()
#endif
#else
#define RANGES_CPU_ENTROPY 0
#endif
#endif
RANGES_DIAGNOSTIC_PUSH
RANGES_DIAGNOSTIC_IGNORE_PRAGMAS
RANGES_DIAGNOSTIC_IGNORE_CXX17_COMPAT
namespace ranges
{
inline namespace v3
{
/// \addtogroup group-concepts
/// @{
namespace concepts
{
struct UniformRandomNumberGenerator
{
template<typename Gen>
using result_t = result_of_t<Gen&()>;
template<typename Gen, typename Result = result_t<Gen>>
auto requires_() -> decltype(
concepts::valid_expr(
concepts::model_of<UnsignedIntegral, Result>(),
concepts::has_type<Result>(uncvref_t<Gen>::min()),
concepts::has_type<Result>(uncvref_t<Gen>::max()),
concepts::is_true(meta::bool_<
(uncvref_t<Gen>::min() < uncvref_t<Gen>::max())>())
));
};
}
template<typename Gen>
using UniformRandomNumberGenerator =
concepts::models<concepts::UniformRandomNumberGenerator, Gen>;
/// @}
/// \cond
namespace detail
{
namespace randutils
{
template<typename T,
CONCEPT_REQUIRES_(Integral<T>())>
RANGES_CXX14_CONSTEXPR std::uint32_t crushto32(T value)
RANGES_INTENDED_MODULAR_ARITHMETIC
{
if(sizeof(T) <= 4)
return static_cast<std::uint32_t>(value);
else {
auto result = static_cast<std::uint64_t>(value);
result *= 0xbc2ad017d719504d;
return static_cast<std::uint32_t>(result ^ (result >> 32));
}
}
template<typename T>
RANGES_CXX14_CONSTEXPR std::uint32_t hash(T && value)
{
auto hasher = std::hash<uncvref_t<T>>{};
return randutils::crushto32(hasher(static_cast<T&&>(value)));
}
constexpr std::uint32_t fnv(std::uint32_t hash, const char* pos)
RANGES_INTENDED_MODULAR_ARITHMETIC
{
return *pos == '\0' ? hash : randutils::fnv(
(hash * 16777619U) ^ static_cast<unsigned char>(*pos), pos+1);
}
constexpr std::size_t weird_seed_sources = 11;
constexpr std::size_t seed_count = weird_seed_sources + 8;
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 6
inline
#else
template<class = void>
#endif
std::array<std::uint32_t, seed_count> local_entropy(std::uint32_t s1, std::uint32_t s2)
{
CONCEPT_ASSERT(seed_count >= weird_seed_sources);
std::array<std::uint32_t, seed_count> seeds;
auto it = seeds.begin();
static std::atomic<std::uint32_t> counter;
*it++ = counter.fetch_add(std::uint32_t{0xedf19156}, std::memory_order_relaxed);
// The heap can vary from run to run as well.
void* malloc_addr = std::malloc(sizeof(int));
std::free(malloc_addr);
*it++ = randutils::hash(malloc_addr);
*it++ = randutils::hash(&malloc_addr);
// Classic seed, the time. It ought to change, especially since
// this is (hopefully) nanosecond resolution time.
auto hitime = std::chrono::high_resolution_clock::now()
.time_since_epoch().count();
*it++ = randutils::crushto32(hitime);
// Address of the thing being initialized. That can mean that
// different seed sequences in different places in memory will be
// different. Even for the same object, it may vary from run to
// run in systems with ASLR, such as OS X, but on Linux it might not
// unless we compile with -fPIC -pic.
*it++ = s1;
// The address of the time function. It should hopefully be in
// a system library that hopefully isn't always in the same place
// (might not change until system is rebooted though)
*it++ = randutils::hash(&std::chrono::high_resolution_clock::now);
// The address of the exit function. It should hopefully be in
// a system library that hopefully isn't always in the same place
// (might not change until system is rebooted though). Hopefully
// it's in a different library from time_func.
{
using namespace std;
*it++ = randutils::hash(&_Exit);
}
// The address of a local function. That may be in a totally
// different part of memory. On OS X it'll vary from run to run thanks
// to ASLR, on Linux it might not unless we compile with -fPIC -pic.
// Need the cast because it's an overloaded function and we need to
// pick the right one.
*it++ = randutils::hash(
static_cast<std::uint32_t(*)(std::uint64_t)>(&randutils::crushto32));
#if RANGES_CXX_THREAD >= RANGES_CXX_THREAD_11
// Hash our thread id. It seems to vary from run to run on OS X, not
// so much on Linux.
*it++ = randutils::hash(std::this_thread::get_id());
#endif
// Hash of the ID of a type. May or may not vary, depending on
// implementation.
*it++ = s2;
// Platform-specific entropy
*it++ = randutils::crushto32(RANGES_CPU_ENTROPY);
RANGES_ASSERT(static_cast<std::size_t>(it - seeds.begin()) <= weird_seed_sources);
// Hopefully high-quality entropy from random_device.
std::random_device rd{};
ranges::generate(it, seeds.end(), ranges::ref(rd));
return seeds;
}
template<typename I, CONCEPT_REQUIRES_(UnsignedIntegral<I>())>
constexpr I fast_exp(I x, I power, I result = I{1})
{
return power == I{0} ? result
: randutils::fast_exp(x * x, power >> 1, result * (power & I{1} ? x : 1));
}
//////////////////////////////////////////////////////////////////////////////
//
// seed_seq_fe
//
//////////////////////////////////////////////////////////////////////////////
/*
* seed_seq_fe implements a fixed-entropy seed sequence; it conforms to all
* the requirements of a Seed Sequence concept.
*
* seed_seq_fe<N> implements a seed sequence which seeds based on a store of
* N * 32 bits of entropy. Typically, it would be initialized with N or more
* integers.
*
* seed_seq_fe128 and seed_seq_fe256 are provided as convenience typedefs for
* 128- and 256-bit entropy stores respectively. These variants outperform
* std::seed_seq, while being better mixing the bits it is provided as entropy.
* In almost all common use cases, they serve as better drop-in replacements
* for seed_seq.
*
* Technical details
*
* Assuming it constructed with M seed integers as input, it exhibits the
* following properties
*
* * Diffusion/Avalanche: A single-bit change in any of the M inputs has a
* 50% chance of flipping every bit in the bitstream produced by generate.
* Initializing the N-word entropy store with M words requires O(N * M)
* time precisely because of the avalanche requirements. Once constructed,
* calls to generate are linear in the number of words generated.
*
* * Bias freedom/Bijection: If M == N, the state of the entropy store is a
* bijection from the M inputs (i.e., no states occur twice, none are
* omitted). If M > N the number of times each state can occur is the same
* (each state occurs 2**(32*(M-N)) times, where ** is the power function).
* If M < N, some states cannot occur (bias) but no state occurs more
* than once (it's impossible to avoid bias if M < N; ideally N should not
* be chosen so that it is more than M).
*
* Likewise, the generate function has similar properties (with the entropy
* store as the input data). If more outputs are requested than there is
* entropy, some outputs cannot occur. For example, the Mersenne Twister
* will request 624 outputs, to initialize its 19937-bit state, which is
* much larger than a 128-bit or 256-bit entropy pool. But in practice,
* limiting the Mersenne Twister to 2**128 possible initializations gives
* us enough initializations to give a unique initialization to trillions
* of computers for billions of years. If you really have 624 words of
* *real* high-quality entropy you want to use, you probably don't need
* an entropy mixer like this class at all. But if you *really* want to,
* nothing is stopping you from creating a randutils::seed_seq_fe<624>.
*
* * As a consequence of the above properties, if all parts of the provided
* seed data are kept constant except one, and the remaining part is varied
* through K different states, K different output sequences will be produced.
*
* * Also, because the amount of entropy stored is fixed, this class never
* performs dynamic allocation and is free of the possibility of generating
* an exception.
*
* Ideas used to implement this code include hashing, a simple PCG generator
* based on an MCG base with an XorShift output function and permutation
* functions on tuples.
*
* More detail at
* http://www.pcg-random.org/posts/developing-a-seed_seq-alternative.html
*/
template<std::size_t count = 4, typename IntRep = std::uint32_t,
std::size_t mix_rounds = 1 + (count <= 2)>
struct seed_seq_fe {
public:
CONCEPT_ASSERT(UnsignedIntegral<IntRep>());
typedef IntRep result_type;
private:
static constexpr std::uint32_t INIT_A = 0x43b0d7e5;
static constexpr std::uint32_t MULT_A = 0x931e8875;
static constexpr std::uint32_t INIT_B = 0x8b51f9dd;
static constexpr std::uint32_t MULT_B = 0x58f38ded;
static constexpr std::uint32_t MIX_MULT_L = 0xca01f9dd;
static constexpr std::uint32_t MIX_MULT_R = 0x4973f715;
static constexpr std::uint32_t XSHIFT = sizeof(IntRep)*8/2;
std::array<IntRep, count> mixer_;
template<typename I, typename S,
CONCEPT_REQUIRES_(InputIterator<I>() && Sentinel<S, I>() &&
ConvertibleTo<reference_t<I>, IntRep>())>
void mix_entropy(I begin, S end)
{
auto hash_const = INIT_A;
auto hash = [&](IntRep value) RANGES_INTENDED_MODULAR_ARITHMETIC
{
value ^= hash_const;
hash_const *= MULT_A;
value *= hash_const;
value ^= value >> XSHIFT;
return value;
};
auto mix = [](IntRep x, IntRep y) RANGES_INTENDED_MODULAR_ARITHMETIC
{
IntRep result = MIX_MULT_L*x - MIX_MULT_R*y;
result ^= result >> XSHIFT;
return result;
};
for(auto& elem : mixer_)
{
if(begin != end)
elem = hash(static_cast<IntRep>(*begin++));
else
elem = hash(IntRep{0});
}
for(auto& src : mixer_)
for(auto& dest : mixer_)
if(&src != &dest)
dest = mix(dest,hash(src));
for(; begin != end; ++begin)
for(auto& dest : mixer_)
dest = mix(dest,hash(static_cast<IntRep>(*begin)));
}
public:
seed_seq_fe(const seed_seq_fe&) = delete;
void operator=(const seed_seq_fe&) = delete;
template<typename T,
CONCEPT_REQUIRES_(ConvertibleTo<T const&, IntRep>())>
seed_seq_fe(std::initializer_list<T> init)
{
seed(init.begin(), init.end());
}
template<typename I, typename S,
CONCEPT_REQUIRES_(InputIterator<I>() && Sentinel<S, I>() &&
ConvertibleTo<reference_t<I>, IntRep>())>
seed_seq_fe(I begin, S end)
{
seed(begin, end);
}
// generating functions
template<typename I, typename S,
CONCEPT_REQUIRES_(RandomAccessIterator<I>() && Sentinel<S, I>())>
void generate(I dest_begin, S dest_end) const
RANGES_INTENDED_MODULAR_ARITHMETIC
{
auto src_begin = mixer_.begin();
auto src_end = mixer_.end();
auto src = src_begin;
auto hash_const = INIT_B;
for(auto dest = dest_begin; dest != dest_end; ++dest)
{
auto dataval = *src;
if(++src == src_end)
src = src_begin;
dataval ^= hash_const;
hash_const *= MULT_B;
dataval *= hash_const;
dataval ^= dataval >> XSHIFT;
*dest = dataval;
}
}
constexpr std::size_t size() const
{
return count;
}
template<typename O,
CONCEPT_REQUIRES_(WeaklyIncrementable<O>() &&
IndirectlyCopyable<decltype(mixer_.begin()), O>())>
void param(O dest) const
RANGES_INTENDED_MODULAR_ARITHMETIC
{
const IntRep INV_A = randutils::fast_exp(MULT_A, IntRep(-1));
const IntRep MIX_INV_L = randutils::fast_exp(MIX_MULT_L, IntRep(-1));
auto mixer_copy = mixer_;
for(std::size_t round = 0; round < mix_rounds; ++round)
{
// Advance to the final value. We'll backtrack from that.
auto hash_const = INIT_A*randutils::fast_exp(MULT_A, IntRep(count * count));
for(auto src = mixer_copy.rbegin(); src != mixer_copy.rend(); ++src)
for(auto dest = mixer_copy.rbegin(); dest != mixer_copy.rend();
++dest)
if(src != dest)
{
IntRep revhashed = *src;
auto mult_const = hash_const;
hash_const *= INV_A;
revhashed ^= hash_const;
revhashed *= mult_const;
revhashed ^= revhashed >> XSHIFT;
IntRep unmixed = *dest;
unmixed ^= unmixed >> XSHIFT;
unmixed += MIX_MULT_R*revhashed;
unmixed *= MIX_INV_L;
*dest = unmixed;
}
for(auto i = mixer_copy.rbegin(); i != mixer_copy.rend(); ++i)
{
IntRep unhashed = *i;
unhashed ^= unhashed >> XSHIFT;
unhashed *= randutils::fast_exp(hash_const, IntRep(-1));
hash_const *= INV_A;
unhashed ^= hash_const;
*i = unhashed;
}
}
ranges::copy(mixer_copy, dest);
}
template<typename I, typename S,
CONCEPT_REQUIRES_(InputIterator<I>() && Sentinel<S, I>() &&
ConvertibleTo<reference_t<I>, IntRep>())>
void seed(I begin, S end)
{
mix_entropy(begin, end);
// For very small sizes, we do some additional mixing. For normal
// sizes, this loop never performs any iterations.
for(std::size_t i = 1; i < mix_rounds; ++i)
stir();
}
seed_seq_fe& stir()
{
mix_entropy(mixer_.begin(), mixer_.end());
return *this;
}
};
using seed_seq_fe128 = seed_seq_fe<4, std::uint32_t>;
using seed_seq_fe256 = seed_seq_fe<8, std::uint32_t>;
//////////////////////////////////////////////////////////////////////////////
//
// auto_seeded
//
//////////////////////////////////////////////////////////////////////////////
/*
* randutils::auto_seeded
*
* Extends a seed sequence class with a nondeterministic default constructor.
* Uses a variety of local sources of entropy to portably initialize any
* seed sequence to a good default state.
*
* In normal use, it's accessed via one of the following type aliases, which
* use seed_seq_fe128 and seed_seq_fe256 above.
*
* randutils::auto_seed_128
* randutils::auto_seed_256
*
* It's discussed in detail at
* http://www.pcg-random.org/posts/simple-portable-cpp-seed-entropy.html
* and its motivation (why you can't just use std::random_device) here
* http://www.pcg-random.org/posts/cpps-random_device.html
*/
template<typename SeedSeq>
struct auto_seeded : public SeedSeq {
auto_seeded()
: auto_seeded(randutils::local_entropy(
randutils::hash(this), randutils::crushto32(typeid(*this).hash_code())))
{}
template<std::size_t N>
auto_seeded(std::array<std::uint32_t, N> const& seeds)
: SeedSeq(seeds.begin(), seeds.end())
{}
using SeedSeq::SeedSeq;
const SeedSeq& base() const
{
return *this;
}
SeedSeq& base()
{
return *this;
}
};
using auto_seed_128 = auto_seeded<seed_seq_fe128>;
using auto_seed_256 = auto_seeded<seed_seq_fe256>;
}
using default_URNG =
meta::if_c<(sizeof(void*) >= 8), std::mt19937_64, std::mt19937>;
#if !RANGES_CXX_THREAD_LOCAL
template<typename URNG>
class sync_URNG
: private URNG
{
mutable std::mutex mtx_;
public:
using URNG::URNG;
sync_URNG() = default;
using typename URNG::result_type;
result_type operator()()
{
std::lock_guard<std::mutex> guard{mtx_};
return static_cast<URNG &>(*this)();
}
using URNG::min;
using URNG::max;
};
using default_random_engine = sync_URNG<default_URNG>;
#else
using default_random_engine = default_URNG;
#endif
template<typename T = void>
default_random_engine& get_random_engine()
{
using Seeder = meta::if_c<
(sizeof(default_URNG) > 16),
randutils::auto_seed_256,
randutils::auto_seed_128>;
#if RANGES_CXX_THREAD_LOCAL >= RANGES_CXX_THREAD_LOCAL_11
static thread_local default_random_engine engine{Seeder{}.base()};
#elif RANGES_CXX_THREAD_LOCAL
static __thread bool initialized = false;
static __thread meta::_t<std::aligned_storage<
sizeof(default_random_engine),
alignof(default_random_engine)>> storage;
if(!initialized)
{
::new(static_cast<void*>(&storage)) default_random_engine{Seeder{}.base()};
initialized = true;
}
auto& engine = reinterpret_cast<default_random_engine&>(storage);
#else
static default_random_engine engine{Seeder{}.base()};
#endif // RANGES_CXX_THREAD_LOCAL
return engine;
}
}
/// \endcond
}
}
RANGES_DIAGNOSTIC_POP
#undef RANGES_CPU_ENTROPY
#endif
|