cppx-core
random-numbers-util.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
3 #include <cppx-core/language/syntax/macro-use.hpp> // CPPX_USE_...
4 
5 #ifndef CPPX_NO_FIX_OF_RANDOM_DEVICE
6 # ifdef __GNUC__
7 # undef _GLIBCXX_USE_RANDOM_TR1
8 # define _GLIBCXX_USE_RANDOM_TR1
9 # endif
10 #endif
11 
12 #include <random>
13 
14 namespace cppx::rnd
15 {
17  invoke,
18  default_random_engine, random_device, uniform_real_distribution
19  );
20 
21  inline auto hardware_entropy()
22  -> unsigned
23  {
24  static random_device the_entropy_source;
25  return the_entropy_source();
26  }
27 
28  template< unsigned value >
29  struct Seed
30  {
31  auto operator()() const
32  -> unsigned
33  { return value; }
34  };
35 
36  struct Random_seed
37  {
38  auto operator()() const
39  -> unsigned
40  { return hardware_entropy(); }
41  };
42 
43  template< class Seeding >
44  class Seq_
45  {
46  public:
47  static inline auto bits_generator()
48  -> default_random_engine&
49  {
50  static default_random_engine the_generator( invoke( []() -> unsigned
51  {
52  const Seeding the_seeder;
53  return the_seeder();
54  } ) );
55 
56  return the_generator;
57  }
58 
59  static inline auto next()
60  -> double
61  {
62  static uniform_real_distribution the_distribution;
63  return the_distribution( bits_generator() );
64  }
65  };
66 
68 
69 } // namespace cppx::rnd
auto operator()() const -> unsigned
CPPX_USE_STD(invoke, default_random_engine, random_device, uniform_real_distribution)
static auto next() -> double
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...
static auto bits_generator() -> default_random_engine &
auto operator()() const -> unsigned
auto hardware_entropy() -> unsigned