forked from wolfgangmauerer/libtrevisan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prng.hpp
29 lines (23 loc) · 747 Bytes
/
prng.hpp
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
#ifndef PRNG_H
#define PRNG_H
#include <random>
#include <vector>
#include <cstdint>
#include <functional>
template<class T>
void *create_randomness(uint64_t n, std::vector<T> &rand) {
// Prepare n bits of randomness using a pseudo-random number generator.
// A vector instead of a raw array is used to store the data because
// this way, it's easier for the caller to keep track of the allocation.
std::uniform_int_distribution<T>
distribution(0, std::numeric_limits<T>::max());
std::mt19937 mt_engine(42);
auto generator = std::bind(distribution, mt_engine);
uint64_t num_chunks = n/sizeof(rand[0])+1;
rand.reserve(num_chunks);
for (auto i = 0; i < num_chunks; i++) {
rand[i] = generator();
}
return (&rand[0]);
}
#endif