Professional Documents
Culture Documents
Noteed
Noteed
Noteed
#include <ctime>
#include <iostream>
#include <unistd.h>
return tmp_s;
}
I've added the lambda in here, but the principle is that you could pass it in and
thereby control what characters your string contains:
Because you can produce strings that follow a certain probability distribution (or
distribution combination) for the character set you're interested in.
Because it has built-in support for non-deterministic random numbers
Because it supports unicode, so you could change this to an internationalized
version.
For example:
#include <iostream>
#include <vector>
#include <random>
#include <functional> //for std::function
#include <algorithm> //for std::generate_n
char_array charset()
{
//Change this to suit
return char_array(
{'0','1','2','3','4',
'5','6','7','8','9',
'A','B','C','D','E','F',
'G','H','I','J','K',
'L','M','N','O','P',
'Q','R','S','T','U',
'V','W','X','Y','Z',
'a','b','c','d','e','f',
'g','h','i','j','k',
'l','m','n','o','p',
'q','r','s','t','u',
'v','w','x','y','z'
});
};
int main()
{
//0) create the character set.
// yes, you can use an array here,
// but a function is cleaner and more flexible
const auto ch_set = charset();
//4) set the length of the string you want and profit!
auto length = 5;
std::cout<<random_string(length,randchar)<<std::endl;
return 0;
}
Sample output.