How do I create a random alpha-numeric string in C

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I d like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string.

How do I do this in C++?

Answers

http://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c#440147 http://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c#440147

void gen_random(char *s, const int len) {
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 0;
}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils