1 /* 2 * Copyright (c) 2019-2020 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #ifndef ARM_COMPUTE_MISC_RANDOM_H 25 #define ARM_COMPUTE_MISC_RANDOM_H 26 27 #include "arm_compute/core/Error.h" 28 29 #include <random> 30 #include <type_traits> 31 32 namespace arm_compute 33 { 34 namespace utils 35 { 36 namespace random 37 { 38 /** Uniform distribution within a given number of sub-ranges 39 * 40 * @tparam T Distribution primitive type 41 */ 42 template <typename T> 43 class RangedUniformDistribution 44 { 45 public: 46 using DT = typename std::conditional<std::is_integral<T>::value, 47 std::uniform_int_distribution<T>, 48 std::uniform_real_distribution<float>>::type; 49 using result_type = T; 50 using range_pair = std::pair<result_type, result_type>; 51 52 public: 53 /** Constructor 54 * 55 * @param[in] low lowest value in the range (inclusive) 56 * @param[in] high highest value in the range (inclusive for uniform_int_distribution, exclusive for uniform_real_distribution) 57 * @param[in] exclude_ranges Ranges to exclude from the generator 58 */ RangedUniformDistribution(result_type low,result_type high,const std::vector<range_pair> & exclude_ranges)59 RangedUniformDistribution(result_type low, result_type high, const std::vector<range_pair> &exclude_ranges) 60 : _distributions(), _selector() 61 { 62 result_type clow = low; 63 for(const auto &erange : exclude_ranges) 64 { 65 result_type epsilon = std::is_integral<result_type>::value ? 1 : static_cast<result_type>(std::numeric_limits<float>::epsilon()); 66 67 ARM_COMPUTE_ERROR_ON(clow > erange.first || clow >= erange.second); 68 69 _distributions.emplace_back(DT(clow, erange.first - epsilon)); 70 clow = erange.second + epsilon; 71 } 72 ARM_COMPUTE_ERROR_ON(clow > high); 73 _distributions.emplace_back(DT(clow, high)); 74 _selector = std::uniform_int_distribution<uint32_t>(0, _distributions.size() - 1); 75 } 76 /** Generate random number 77 * 78 * @tparam URNG Random number generator object type 79 * 80 * @param[in] g A uniform random number generator object, used as the source of randomness. 81 * 82 * @return A new random number. 83 */ 84 template <class URNG> operator()85 result_type operator()(URNG &g) 86 { 87 unsigned int rand_select = _selector(g); 88 return _distributions[rand_select](g); 89 } 90 91 private: 92 std::vector<DT> _distributions; 93 std::uniform_int_distribution<uint32_t> _selector; 94 }; 95 } // namespace random 96 } // namespace utils 97 } // namespace arm_compute 98 #endif /* ARM_COMPUTE_MISC_RANDOM_H */ 99