vendredi 26 janvier 2018

Random number return the same numbers c++ [duplicate]

I want a vector that is filled automatically with random letters without repeating any, but whenever I compile the program it generates the same letters in the same positions

this is my code:

string alphabet[27] = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","Ñ","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
string vector[20];
int pos[20];
int r;
bool rep = false;

int main() {

    cout << "Generate vector" << endl;

    for (int i = 0; i < 20; i++) {

        do {
        rep = false;
        r = rand() % 27;
        vector[i] = alphabet[r];
        pos[i] = r;

                for (int j = 0; j < i; j++) {
                if (r == pos[j])
                    rep= true;
            }
        } while (rep == true);
    }

    for (int i = 0; i < 20; i++) {
        cout << i << " . " << vector[i] << endl;
    }

    _getch();

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire