This is my random password generator. It relies on the boost's random_device for entropy that it feeds to uniform_int_distribution. It is a variant of non-deterministic random number generation.
// Random_password.cpp
/*
This code generates a random character password of length as specified on commandline.
Else it generates upto 8 characters.
Using a model for a non-deterministic random number generator.
*/
//Boost random lib
#include <boost/random/random_device.hpp>//for entropy
#include <boost/random/uniform_int_distribution.hpp>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
if(argc = 2){
//cout << "Usage: random_pass 10\n"<<endl;
auto length = atoi(argv[1]);
}else{
auto length = 8;
}
string chars(//Chars we are going to allow. Normal keyboard characters
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()"
"`~-_=+[{]{\\|;:'\",<.>/? ");
//random_device is for entropy, for a non-predictable password.
boost::random::random_device rng;
//Now generate random characters of specified length of charaters from our chars string.
boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);//returns a random integer on each invocation froma set of integers
for(int i = 0; i < length; ++i) {
cout << chars[index_dist(rng)];
}
cout << endl;
}
[code]