Say we have an enumerated type E
enum class E : underlying_type_of_E { v1 = uE1, v2 = uE2,
In the general case, not all uE values ββare valid values ββof E , because we can choose a relation between them. Is there a general way to create random, real (named in the definition, not assigned) values ββof E? This, for example, would not work:
std::mt19937 m; std::uniform_int_distribution<uE> randomUE(0, std::numeric_limits<uE>::max()); E e = static_cast<E>( randomUE(m) );
because:
- The range of values ββmay not start at 0
- The range of values ββmay not end with std :: numeric_limits :: max ()
- The range of values ββmay not be a range at all - we can select discrete values ββfor E from uE, for example {1, 3, 64, 272}.
Given that all of the listed values ββare known at compile time, I cannot imagine why this would be dangerous or error prone.
As for the context, why I want this - I'm working on a genetic algorithm that uses a ratchet repository of verbs. At the moment, I use enumerations as chromosomes and save them in std::vector<bool> , which, upon request, is converted to std::vector<enumT> . The problem with this approach is a mutation that flips random bits with a given probability. This can cause problems, as it can lead to unacceptable chromosomes that do not indicate enum values.
source share