jeudi 27 décembre 2018

Generate random letters

string bolsa_letras::letters_generator(int quantity){
    int already_generated = 0;
    map<char, int> aux = values;
    string out;
    while(already_generated != quantity){
        char generated_char = 'A' + rand()%26;
        if(aux[generated_char] > 0){
            out.push_back(generated_char);
            aux[generated_char]--;
            already_generated++;
        }
    }

    return out;
}

Above is the code that given a number generates random letters.

The map saves the letters and the times that letters can be appeared. The problem is that every time i run the code, it prints the same: NLRBBMQH. Why is so?

I have include cstdlib for the rand function.




Aucun commentaire:

Enregistrer un commentaire