Random of Enumerator

1

I'm not able to do a random enumerator.

enum Notas {A=10, B=22, C=31, D=44, E=56};

It has this solution, but I need the enumerator values and here it is returning the rest of the calculation.

enum Notas {A=10, B=22, C=31, D=44, E=56, ULTIMO};


int teste = static_cast<Notas>(rand() % ULTIMO);
    
asked by anonymous 09.03.2016 / 18:00

1 answer

1

The problem seems to be that the possible values are not all valid. Instead of generating the random number directly, you will need a conversion.

Generate the number according to the number of Notes elements. If you have 5 grades, from A to E, generate a number from 0 to 4 using the:

enum Notas {A=10, B=22, C=31, D=44, E=56, ULTIMO};
#define NUMERO_DE_NOTAS 5

int teste = static_cast<Notas>(rand() % NUMERO_DE_NOTAS);

After that, you use the test to enter a switch and with each value return the correct note. If it is 0, it returns A, if it is 1, it returns B, etc ...

    
09.03.2016 / 18:15