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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#pragma once
#include "globalincs/pstypes.h"
#include "parse/parselo.h"
#include <random>
#include <type_traits>
#include <limits>
namespace util {
/**
* @defgroup randomUtils Random Utilities
*
* Utility functions for handling random values
*/
namespace {
/**
* @brief Parses a generic list of numbers
* @param list The array where the numbers should be stored.
* @return The amount of parsed numbers
*
* @ingroup randomUtils
*/
template<typename T, size_t N>
size_t parse_number_list(T (& list)[N]) {
ignore_white_space();
if (*Mp != '(')
{
// Probably one a single value so stuff that and don't parse a list. This makes it easier to specify single values
float val;
stuff_float(&val);
list[0] = static_cast<T>(val);
return 1;
}
float helpList[N];
auto num = stuff_float_list(helpList, N);
for (size_t i = 0; i < num; ++i) {
list[i] = static_cast<T>(helpList[i]);
}
return num;
}
}
/**
* @brief Generic class for generating numbers in a specific range
*
* This allows to use a generic value, distribution and generator type. It's valid to only use one value for the range
* in which case the returned value is constant.
*
* @ingroup randomUtils
*/
template<typename Value, typename Distribution, typename Generator>
class RandomRange {
public:
typedef Distribution DistributionType;
typedef Generator GeneratorType;
typedef Value ValueType;
private:
mutable GeneratorType m_generator;
mutable DistributionType m_distribution;
bool m_constant;
ValueType m_minValue;
ValueType m_maxValue;
public:
template<typename... Ts>
RandomRange(ValueType param1, ValueType param2, Ts&& ... distributionParameters) :
m_generator(std::random_device()()),
m_distribution(param1, param2, distributionParameters...) {
m_minValue = static_cast<ValueType>(param1);
m_maxValue = static_cast<ValueType>(param2);
m_constant = false;
}
explicit RandomRange(const ValueType& val) : RandomRange() {
m_minValue = val;
m_maxValue = val;
m_constant = true;
}
RandomRange() :
m_generator(std::random_device()()),
m_distribution() {
m_minValue = static_cast<ValueType>(0.0);
m_maxValue = static_cast<ValueType>(0.0);
m_constant = true;
}
/**
* @brief Determines the next random number of this range
* @return The random number
*/
ValueType next() const {
if (m_constant) {
return m_minValue;
}
return m_distribution(m_generator);
}
/**
* @brief Gets the minimum value that may be returned by this random range
*
* @warning This is not valid for normal distribution ranges since those do not have a definite minimum value.
*
* @return The minimum value
*/
ValueType min() const {
return m_minValue;
}
/**
* @brief Gets the maximum value that may be returned by this random range
*
* @warning This is not valid for normal distribution ranges since those do not have a definite maximum value.
*
* @return The maximum value
*/
ValueType max() {
return m_maxValue;
}
};
/**
* @brief A random range with a normal distribution
*
* The range parameters are passed directly to std::normal_distribution
*
* @ingroup randomUtils
*/
template<typename Value>
using NormalRange = RandomRange<Value,
std::normal_distribution<Value>,
std::minstd_rand>;
/**
* @brief A normal range which uses floats
*
* @ingroup randomUtils
*/
typedef NormalRange<float> NormalFloatRange;
/**
* @brief A function for parsing a normal range
* @return The parsed normal range
*
* @ingroup randomUtils
*/
template<typename Value>
NormalRange<Value> parseNormalRange() {
Value valueList[2];
auto num = parse_number_list(valueList);
if (num == 0) {
error_display(0, "Need at least one value to form a random range!");
return NormalRange<Value>();
}
else if (num == 1) {
return NormalRange<Value>(valueList[0]);
}
return NormalRange<Value>(valueList[0], valueList[1]);
}
/**
* @brief A generic random range which uses a uniform distribution
*
* @ingroup randomUtils
*/
template<typename Value>
using UniformRange = RandomRange<Value,
typename std::conditional<std::is_integral<Value>::value,
std::uniform_int_distribution<Value>,
std::uniform_real_distribution<Value>>::type,
std::minstd_rand>;
/**
* @brief A uniform range which uses floats
*
* @ingroup randomUtils
*/
typedef UniformRange<float> UniformFloatRange;
/**
* @brief A uniform range which uses ints
*
* @ingroup randomUtils
*/
typedef UniformRange<int> UniformIntRange;
/**
* @brief A uniform range which uses uints
*
* @ingroup randomUtils
*/
typedef UniformRange<uint> UniformUIntRange;
/**
* @brief Parses a generic uniform range
* @param allowNegativ If @c true, negative values will be allowed
* @param min The minimum value the random range may return
* @param max The maximum value the random range may return
*
* @return The parsed uniform range
*
* @ingroup randomUtils
*/
template<typename Value>
UniformRange<Value> parseUniformRange(Value min = std::numeric_limits<Value>::min(),
Value max = std::numeric_limits<Value>::max()) {
Assertion(min <= max, "Invalid min-max values specified!");
Value valueList[2];
auto num = parse_number_list(valueList);
if (num == 0) {
error_display(0, "Need at least one value to form a random range!");
return UniformRange<Value>();
}
else if (num == 1) {
return UniformRange<Value>(valueList[0]);
}
if (valueList[0] > valueList[1]) {
error_display(0, "Minimum value %f is more than maximum value %f!", (float) valueList[0], (float) valueList[1]);
std::swap(valueList[0], valueList[1]);
}
if (valueList[0] < min) {
error_display(0, "First value (%f) is less than the minimum %f!", (float) valueList[0], (float) min);
valueList[0] = min;
}
if (valueList[0] > max) {
error_display(0, "First value (%f) is greater than the maximum %f!", (float) valueList[0], (float) max);
valueList[0] = max;
}
if (valueList[1] < min) {
error_display(0, "Second value (%f) is less than the minimum %f!", (float) valueList[1], (float) min);
valueList[1] = min;
}
if (valueList[1] > max) {
error_display(0, "Second value (%f) is greater than the maximum %f!", (float) valueList[1], (float) max);
valueList[1] = max;
}
if (valueList[0] == valueList[1]) {
// If the two values are equal then this is slightly more efficient
return UniformRange<Value>(valueList[0]);
} else {
return UniformRange<Value>(valueList[0], valueList[1]);
}
}
}
|