Code Golf: Passwords


You can learn a lot about how a programming language and its compiler interact by "pushing the limits" with challenges like "Code Golf". Code Golf is best described as attempting to write a program to perform a specified task while using the least amount of code possible. The resulting program is unlikely to be easy to read, but learning how to do tasks in the shortest possible way can teach you a lot about how a programming language works, and it is fun!


Using the fewest characters possible, write a valid C++ program, complying with the C++ 98 or C++ 11 standard, that will generate a strong password of length 16, and display it to the screen. The password must contain exactly 16 characters, and the characters are limited to the four ASCII character classes described below:

  • Alphabetical characters: The set of uppercase English alphabetical characters 'A' through 'Z'.
  • Alphabetical characters: The set of lowercase English alphabetical characters 'a' through 'z'.
  • Digits: The digits '0' through '9'.
  • The punctuation characters in the set [ !#$%&()*+,-./:;<=>?@[]^_{|}~] NOTE: The space character (' ') is included, but no other whitespace character is allowed).

Password Strength Requirements:

  1. The password must contain at least one character from each of the four character classes.
  2. All characters in the password must be chosen such that every possible character has an equal likelihood of being chosen. (In other words, characters should be chosen uniformly from the entire available set, provided that requirement #1 is met by the end of the password.)
  3. There must be no predictable pattern to the placement of the characters from each of the four character classes. (In other words, you cannot fulfill requirement #1 by always choosing an uppercase letter followed by a lowercase letter followed by a digit followed by a symbol, then 12 random characters; or anything similar. The finished password must appear completely random.)

Programs will be evaluated for correctness, and then scored on raw byte count (as reported by the wc utility on Linux/UNIX, for example).


You may use any "tricks" or language features you like to solve this problem, provided you adhere to the following rules:

  • Your program must compile and run on a Linux (Ubuntu) system with the gcc 4.9.1 compiler using the command and compiler flags shown below:

g++ -Wall -Wextra -std=c++11 -pedantic programName.cpp

Where programName.cpp is replaced by your actual program name. No other flags or options may be added.

  • Your program must not produce any compiler warnings when compiled.
  • Your program must end with the newline character as the last character in the file.
  • Your program must not omit the return 0; required by the C++ standard (even though the compiler will not flag it as an error).
  • No input is allowed from any source.
  • Your program must run to successful completion (no crashes, segmentation faults, exceptions, or infinite loops allowed).
  • You may not include any non-standard headers, or link against any non-standard libraries.

Parting Thoughts

Write the code in a readable format first, then carefully remove extra characters like whitespace. Remember, every character counts! Any trick you can use to reduce the number of characters in your source code is fair game (except those tricks disallowed by the limitations above).

Have fun!