1 /* 2 SPDX-FileCopyrightText: 2018 Mathias Kraus <k.hias@gmx.de> 3 4 SPDX-License-Identifier: GPL-2.0-or-later 5 */ 6 7 #ifndef _GRANATIER_RANDOM_H_ 8 #define _GRANATIER_RANDOM_H_ 9 10 #include <random> 11 12 namespace granatier { 13 namespace RNG { 14 15 /** 16 * @brief random number generator 17 * 18 * @param min: the min value of the range (inclusive) 19 * @param max: the max value of the range (inclusive) 20 * @return T: the random number 21 */ 22 template <typename T> 23 T fromRange(T min, T max, typename std::enable_if<std::is_integral<T>::value >::type* = nullptr) { 24 static std::random_device randomDevice; 25 std::uniform_int_distribution<T> distribution(min, max); 26 return distribution(randomDevice); 27 } 28 29 /** 30 * @brief random number generator 31 * 32 * @param min: the min value of the range (inclusive) 33 * @param max: the max value of the range (inclusive) 34 * @return T: the random number 35 */ 36 template <typename T> 37 T fromRange(T min, T max, typename std::enable_if<std::is_floating_point<T>::value >::type* = nullptr) { 38 static std::random_device randomDevice; 39 std::uniform_real_distribution<T> distribution(min, max); 40 return distribution(randomDevice); 41 } 42 43 } // namespace RNG 44 } // namespace granatier 45 46 #endif // _GRANATIER_RANDOM_H_ 47