File indexing completed on 2024-04-07 03:56:35

0001 /*
0002     SPDX-FileCopyrightText: 2018 Mathias Kraus <k.hias@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef _GRANATIER_RANDOM_H_
0008 #define _GRANATIER_RANDOM_H_
0009 
0010 #include <random>
0011 
0012 namespace granatier {
0013 namespace  RNG {
0014 
0015 /**
0016 * @brief random number generator
0017 * 
0018 * @param min: the min value of the range (inclusive)
0019 * @param max: the max value of the range (inclusive)
0020 * @return T: the random number
0021 */
0022 template <typename T>
0023 T fromRange(T min, T max, typename std::enable_if<std::is_integral<T>::value >::type* = nullptr) {
0024 static std::random_device randomDevice;
0025     std::uniform_int_distribution<T> distribution(min, max);
0026     return distribution(randomDevice);
0027 }
0028 
0029 /**
0030 * @brief random number generator
0031 * 
0032 * @param min: the min value of the range (inclusive)
0033 * @param max: the max value of the range (inclusive)
0034 * @return T: the random number
0035 */
0036 template <typename T>
0037 T fromRange(T min, T max, typename std::enable_if<std::is_floating_point<T>::value >::type* = nullptr) {
0038 static std::random_device randomDevice;
0039     std::uniform_real_distribution<T> distribution(min, max);
0040     return distribution(randomDevice);
0041 }
0042 
0043 } // namespace RNG
0044 } // namespace granatier
0045 
0046 #endif // _GRANATIER_RANDOM_H_