Lumiera  0.pre.03
»edit your freedom«
random.hpp
Go to the documentation of this file.
1 /*
2  RANDOM.hpp - support for random number generation with controlled seed
3 
4  Copyright (C) Lumiera.org
5  2024, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
36 #ifndef LIB_RANDOM_H
37 #define LIB_RANDOM_H
38 
39 
40 #include "lib/integral.hpp"
41 #include "lib/nocopy.hpp"
42 
43 #include <random>
44 
45 
46 namespace lib {
47 
50  {
51  public:
52  virtual ~SeedNucleus();
53  virtual uint64_t getSeed() =0;
54  };
55 
64  template<class GEN>
67  {
68  std::uniform_int_distribution<int> uniformI_;
69  std::uniform_int_distribution<uint64_t> uniformU_;
70  std::uniform_real_distribution<double> uniformD_;
71 
72  GEN generator_;
73 
74  public:
77 
78  int i32() { return uniformI_(generator_); }
79  uint64_t u64() { return uniformU_(generator_); }
80  double uni() { return uniformD_(generator_); }
81 
83  void randomise(SeedNucleus&);
84  };
85 
90 
91 
93  extern Random defaultGen;
94 
96  extern Random entropyGen;
97 
98 
99  /* ===== convenience accessors ===== */
100 
101  inline int rani() { return defaultGen.i32(); }
102  inline uint64_t ranu() { return defaultGen.u64(); }
103  inline double runi() { return defaultGen.uni(); }
104 
105 
107  void randomiseRandomness();
108 
109 
110  /* ===== Implementation details ===== */
111 
112  template<class GEN>
113  inline
115  : uniformI_{0}
116  , uniformU_{0}
117  , uniformD_{}
118  , generator_{nucleus.getSeed()}
119  { }
120 
121 
122  template<class GEN>
123  inline void
125  {
126  generator_.discard (nucleus.getSeed() % 55555);
127  }
128 
129 
130 } // namespace lib
131 #endif /*LIB_RANDOM_H*/
void randomise(SeedNucleus &)
inject controlled randomisation
Definition: random.hpp:124
virtual ~SeedNucleus()
this is an interface
Definition: random.cpp:78
Access point to a selection of random number sources.
Definition: random.hpp:65
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
RandomSequencer(SeedNucleus &)
Random instances are created as part of an execution scheme.
Definition: random.hpp:114
Random entropyGen
a global RandomSequencer seeded with real entropy
Definition: random.cpp:81
Implementation namespace for support and library code.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Establishes a seed point for any instance or performance.
Definition: random.hpp:49
Random defaultGen
a global default RandomSequencer for mundane purposes
Definition: random.cpp:80
void randomiseRandomness()
inject true randomness into the defaultGen
Definition: random.cpp:85