Random C++ STL-only password generator for education This is some code I wrote five years ago and I'm now using it partially as instructional material. I think there are some things that are 'off' about it, namely the top of the main function being the definition of a few variables instead of the argument parsing, and the actual generation not being split into its own function.

#include &lt;chrono&gt;
#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;random&gt;
#include &lt;string&gt;

static void show_usage(std::string name)
{
    std::cerr &lt;&lt; "Usage: " &lt;&lt; name &lt;&lt; " &lt;option(s)&gt;\n"
              &lt;&lt; "Options:\n"
              &lt;&lt; "\t-h,--help\t\tShow this help message\n"
              &lt;&lt; "\t-l,--length LENGTH\tSpecify the password length\n"
              &lt;&lt; "\t-s,--seed SEED\t\tUse a unique seed for the random engine\n"
              &lt;&lt; "\t-v,--version\t\tOutput the program version\n";
}

int main (int argc, char *argv[])
{
    std::string charset ("abcdefghijklmnopqrstuvwxyz"
                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
                         "1234567890!@#$%^&amp;*()");
    int length = 16;
    std::default_random_engine generator;
    std::uniform_int_distribution&lt;int&gt; distribution (0, (int) charset.size() - 1);
    generator.seed (std::chrono::system_clock::now().time_since_epoch().count());

    if (argc &gt;= 2) {
        for (int i = 1; i &lt; argc; i++) {
            std::string arg = argv[i];
            if ((arg ==  "-l") || (arg == "--length")) {
                if (i + 1 &lt; argc) {
                    length = atoi(argv[++i]);
                } else {
                    std::cerr &lt;&lt; "-l,--length takes one argument" &lt;&lt; std::endl;
                    return 1;
                }
            } else if ((arg == "-s") || (arg == "--seed")) {
                if (i + 1 &lt; argc) {
                    std::string str (argv[++i]);
                    std::seed_seq seed (str.begin(), str.end());
                    generator.seed (seed);
                } else {
                    std::cerr &lt;&lt; "-s,--seed takes one argument" &lt;&lt; std::endl;
                    return 1;
                }
            } else if ((arg == "-h") || (arg == "--help")) {
                show_usage(argv[0]);
                return 0;
            } else if ((arg == "-v") || (arg == "--version")) {
                std::cout &lt;&lt; "Version 0.0.1" &lt;&lt; std::endl;
                return 0;
            }
        }
    }

    auto randchar = std::bind (distribution, generator);
    std::string password;
    password.resize(length);

    for (int i = 0; i &lt; length; i++) {
        password.at(i) = charset.at(randchar());
    }

    std::cout &lt;&lt; password &lt;&lt; std::endl;
    return 0;
}
The code looks good. May be argument parsing could be done better, but in general this is solid simple password generator. There are things to mention though, but they mostly apply on reusability and expandability

Separate concerns

At the moment the main() function does quite a lot of things:


Setup random number generator
Parse program arguments
Generate password
Output it to the user


Encapsulate

I'm not saying reduce usability, but the password generator is very much like random number generator, it just gives out strings with random elements instead of bit blocks.

So, in my opinion, the following code would have sound design:

class random_password_generator
{
    //random engine and distribution
public:
    random_password_generator() 
    {
        //initialize
    }

    //random engine will decide rule of 5

    std::string operator()()
    {
        //create password
    }
}




Parsing arguments can be just a plain function. For complex cases may be using Boost.program_options would be better. 



Code Review

atoi(argv[++i]);


atoi is a C standard library function. stoi is a C++ function standard library. In general, C++ library is more compatible with concepts of C++ itself, whereas C library usually needs some wrappers to be usable with the rest of C++.

int length = 16;


My personal preference is to use unsigned int or std::size_t for things that cannot have negative values.

Do not use bind unless necessary

std::bind() might have problems that lambda can't solve, but I'm yet to encounter such problems. In general: use lambda for function objects unless you can't. The reason is that std::bind does heap allocation, type erasure, and possibly other stuff, whereas lambda is just anonymous object with operator().

Range loops

for (int i = 0; i &lt; length; i++)


The above also has a chance to fail in case you'll change the resizing part of the string. It is better to use range for loop:

for (char&amp; c: password)
{
    //assign
}


It will also remove the need to use at(), which does bounds checking, and in general frowned upon in release code.

Even better

Use standard algorithms. Since the functor generates random characters, code could just use std::generate(password.begin(), password.end(), generator).

Do not use std::endl

std::endl is not only a platform independent way to print newline. Printing newline is achieved with streaming '\n'. Even on windows ostream will deal with it. std::endl prints newline and flushes stream, which might cause detrimental performance hit in tight loops.

Don't use C style casts

(int) charset.size()


C style casts are much more dangerous, and compiler will not warn you if it does something dangerous. On the other hand, using one of C++ style casts will ensure compilation error in case something fishy is going on, except reinterpret_cast&lt;&gt;, which is fishy by definition.