1 | /* |
---|---|
2 | * grandom.cc |
3 | * |
4 | * This file is licensed under the GPLv2 or later |
5 | * |
6 | * Pseudo-random number generation |
7 | * |
8 | * Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it> |
9 | * Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de> |
10 | * Copyright (C) 2022 Albert Astals Cid <aacid@kde.org> |
11 | */ |
12 | |
13 | #include "grandom.h" |
14 | |
15 | #include <random> |
16 | |
17 | namespace { |
18 | |
19 | auto &grandom_engine() |
20 | { |
21 | static thread_local std::default_random_engine engine { std::random_device {}() }; |
22 | return engine; |
23 | } |
24 | |
25 | } |
26 | |
27 | void grandom_fill(unsigned char *buff, int size) |
28 | { |
29 | auto &engine = grandom_engine(); |
30 | std::uniform_int_distribution<unsigned short> distribution { std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max() }; |
31 | for (int index = 0; index < size; ++index) { |
32 | buff[index] = static_cast<unsigned char>(distribution(engine)); |
33 | } |
34 | } |
35 | |
36 | double grandom_double() |
37 | { |
38 | auto &engine = grandom_engine(); |
39 | return std::generate_canonical<double, std::numeric_limits<double>::digits>(urng&: engine); |
40 | } |
41 |