/src/poppler/goo/grandom.cc
Line | Count | Source |
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 | 17.8k | { |
21 | 17.8k | static thread_local std::default_random_engine engine { std::random_device {}() }; |
22 | 17.8k | return engine; |
23 | 17.8k | } |
24 | | |
25 | | } |
26 | | |
27 | | void grandom_fill(unsigned char *buff, int size) |
28 | 17.8k | { |
29 | 17.8k | auto &engine = grandom_engine(); |
30 | 17.8k | std::uniform_int_distribution<unsigned short> distribution { std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max() }; |
31 | 303k | for (int index = 0; index < size; ++index) { |
32 | 286k | buff[index] = static_cast<unsigned char>(distribution(engine)); |
33 | 286k | } |
34 | 17.8k | } |
35 | | |
36 | | double grandom_double() |
37 | 0 | { |
38 | 0 | auto &engine = grandom_engine(); |
39 | 0 | return std::generate_canonical<double, std::numeric_limits<double>::digits>(engine); |
40 | 0 | } |