5 #ifndef HERALD_RANDOMNESS_H
6 #define HERALD_RANDOMNESS_H
49 std::string methodName()
const {
53 void nextBytes(std::size_t count,
Data& into) {
54 for (std::size_t i = 0;i < count;i++) {
71 : rd(), gen(rd()), distrib(LONG_MIN,LONG_MAX)
79 distrib(other.distrib)
86 std::string methodName()
const {
87 return "integerdistributed";
90 void nextBytes(std::size_t count,
Data& into) {
91 for (std::size_t i = 0;i < count;i++) {
92 into.
append(std::byte(distrib(gen)));
97 return (
int)distrib(gen);
100 double nextDouble() {
101 return (
double)distrib(gen);
105 std::random_device rd;
107 std::uniform_int_distribution<int64_t> distrib;
125 template <
typename RandomnessSourceT>
129 : m_source(std::move(toOwn)),
137 template <
typename T>
138 void addEntropy(T entropy) {
140 constexpr std::size_t size =
sizeof(T);
145 constexpr std::size_t multiple =
sizeof(std::size_t) / size;
148 std::size_t toXor = 0;
149 for (std::size_t i = 0;i < multiple;i++) {
157 m_entropy = m_entropy ^ toXor;
161 std::string methodName()
const {
162 return m_source.methodName();
165 void nextBytes(std::size_t count,
Data& into) {
166 constexpr std::size_t byteSize =
sizeof(std::byte);
167 constexpr std::size_t sizeTSize =
sizeof(std::size_t);
168 constexpr std::size_t shifts = sizeTSize / byteSize;
170 m_source.nextBytes(count,sourcedInto);
173 for (std::size_t byteIndex = 0;byteIndex < count;byteIndex++) {
175 std::size_t(sourcedInto.
at(byteIndex))
177 (m_entropy >> 8 * (byteIndex % shifts))
183 return (
int)(m_source.nextInt() ^ m_entropy);
186 double nextDouble() {
187 return (
double)(((std::size_t)m_source.nextDouble()) ^ m_entropy);
191 RandomnessSourceT m_source;
192 std::size_t m_entropy;
Definition: randomness.h:43
The main data workhorse class of the Herald API.
Definition: data.h:33
std::byte at(std::size_t index) const
Returns the individual byte at index position, or a byte value of zero if index is out of bounds.
Definition: data.h:178
void append(const DataRef &rawData, std::size_t offset, std::size_t length)
Copies another DataRef into this instance, expanding if required.
Definition: data.h:201
Definition: randomness.h:68
Definition: randomness.h:126
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15