/* * grandom.cc * * This file is licensed under the GPLv2 or later * * Pseudo-random number generation * * Copyright (C) 2012 Fabio D'Urso * Copyright (C) 2018 Adam Reichold * Copyright (C) 2022 Albert Astals Cid */ #include "grandom.h" #include 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 distribution { std::numeric_limits::min(), std::numeric_limits::max() }; for (int index = 0; index < size; ++index) { buff[index] = static_cast(distribution(engine)); } } double grandom_double() { auto &engine = grandom_engine(); return std::generate_canonical::digits>(engine); }