##// END OF EJS Templates
Fix move of an event in another catalogue
Fix move of an event in another catalogue

File last commit:

r1215:cb9ad4b9f97b
r1379:99a785298325
Show More
FuzzingUtils.h
63 lines | 2.3 KiB | text/x-c | CLexer
Alexandre Leroux
Adds utility class to get random values
r1206 #ifndef SCIQLOP_FUZZINGUTILS_H
#define SCIQLOP_FUZZINGUTILS_H
Alexandre Leroux
Adds weights to operations (1)...
r1215 #include <algorithm>
Alexandre Leroux
Adds utility class to get random values
r1206 #include <random>
/**
* Class that proposes random utility methods
*/
class RandomGenerator {
public:
/// @return the unique instance of the random generator
static RandomGenerator &instance();
/// Generates a random double between [min, max]
double generateDouble(double min, double max);
/// Generates a random int between [min, max]
int generateInt(int min, int max);
Alexandre Leroux
Adds weights to operations (1)...
r1215 /**
* Returns a random element among the elements of a container. An item may be more likely to be
* selected if it has an associated weight greater than other items
* @param container the container from which to retrieve an element
* @param weights the weight associated to each element of the container. The vector must have
* the same size of the container for the weights to be effective
* @param nbDraws the number of random draws to perform
* @return the random element retrieved, an element built by default if the container is empty
*/
Alexandre Leroux
Adds utility class to get random values
r1206 template <typename T, typename ValueType = typename T::value_type>
Alexandre Leroux
Adds weights to operations (1)...
r1215 ValueType randomChoice(const T &container, const std::vector<double> &weights = {});
Alexandre Leroux
Adds utility class to get random values
r1206
private:
std::mt19937 m_Mt;
explicit RandomGenerator();
};
Alexandre Leroux
Fixes clang-format for resource files
r1211 template <typename T, typename ValueType>
Alexandre Leroux
Adds weights to operations (1)...
r1215 ValueType RandomGenerator::randomChoice(const T &container, const std::vector<double> &weights)
Alexandre Leroux
Adds utility class to get random values
r1206 {
Alexandre Leroux
Fixes clang-format for resource files
r1211 if (container.empty()) {
Alexandre Leroux
Adds utility class to get random values
r1206 return ValueType{};
}
Alexandre Leroux
Adds weights to operations (1)...
r1215 // Generates weights for each element: if the weights passed in parameter are malformed (the
// number of weights defined is inconsistent with the number of elements in the container, or
// all weights are zero), default weights are used
auto nbIndexes = container.size();
std::vector<double> indexWeights(nbIndexes);
if (weights.size() != nbIndexes || std::all_of(weights.cbegin(), weights.cend(),
[](const auto &val) { return val == 0.; })) {
std::fill(indexWeights.begin(), indexWeights.end(), 1.);
}
else {
std::copy(weights.begin(), weights.end(), indexWeights.begin());
}
// Performs a draw to determine the index to return
std::discrete_distribution<> d{indexWeights.cbegin(), indexWeights.cend()};
return container.at(d(m_Mt));
Alexandre Leroux
Adds utility class to get random values
r1206 }
#endif // SCIQLOP_FUZZINGUTILS