dimanche 4 décembre 2016

How to get random words c++

i need your help about that, i am learning and try to make something but is not nice. So that work good, but not very nice, you know other method much better? I want to generate random word with 10 characters, but if already exist in database to generate again until not exist in database. PS: C++ standart

char GenerateRandom()
{
    const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int len = sizeof(chars) - 1;
    return chars[rand() % len];
}

char strID;

for(unsigned int i=0; i<10; ++i) {
    strID += GenerateRandom();
}

std::auto_ptr<SQLMsg> pMsg(MySQL::instance().DirectQuery("SELECT strID FROM table.list WHERE strID = '%s' ", strID));

while (pMsg->Get()->uiNumRows > 0) /* If already exist a strID with same id in database will be generated again other strID. */
{
    for(unsigned int j=0; j<10; ++j){
        strID += GenerateRandom();
    }
}

//Now all is fine (not exist some id's in database what was generated, now will be insert strID in mysql row with succesfully.

cout << strID;




Aucun commentaire:

Enregistrer un commentaire