-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgrandom.cc
40 lines (33 loc) · 1011 Bytes
/
grandom.cc
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
/*
* grandom.cc
*
* This file is licensed under the GPLv2 or later
*
* Pseudo-random number generation
*
* Copyright (C) 2012 Fabio D'Urso <[email protected]>
* Copyright (C) 2018 Adam Reichold <[email protected]>
* Copyright (C) 2022 Albert Astals Cid <[email protected]>
*/
#include "grandom.h"
#include <random>
namespace {
auto &grandom_engine()
{
static thread_local std::default_random_engine engine { std::random_device {}() };
return engine;
}
}
void grandom_fill(unsigned char *buff, int size)
{
auto &engine = grandom_engine();
std::uniform_int_distribution<unsigned short> distribution { std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max() };
for (int index = 0; index < size; ++index) {
buff[index] = static_cast<unsigned char>(distribution(engine));
}
}
double grandom_double()
{
auto &engine = grandom_engine();
return std::generate_canonical<double, std::numeric_limits<double>::digits>(engine);
}