File: grandom.cc

package info (click to toggle)
poppler 20.09.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 16,092 kB
  • sloc: cpp: 151,577; ansic: 30,155; python: 367; makefile: 42; sh: 36
file content (39 lines) | stat: -rw-r--r-- 927 bytes parent folder | download | duplicates (2)
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
/*
 * grandom.cc
 *
 * This file is licensed under the GPLv2 or later
 *
 * Pseudo-random number generation
 *
 * Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
 * Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
 */

#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] = distribution(engine);
    }
}

double grandom_double()
{
    auto &engine = grandom_engine();
    return std::generate_canonical<double, std::numeric_limits<double>::digits>(engine);
}