Originally Posted by
Salem
Code:
psw.insert(rnd);
if (psw.insert(rnd).second == false)
cout << rnd << "-" << psw.insert(rnd).second << "-";
It seems like the 2nd insert will always fail, because you've already inserted it.
Yes Thanks . Solved
Originally Posted by
laserlight
Code:
uniform_int_distribution<char> random_ascii{'a', 'z'};
MSVC doesn't like 'char' ?I googled a bit but I admit that I dont know the subject. Did you use gcc ?
I read your remarks . Thanks I'll try to improve it
****
I've written the code. I'm not happy with it , but It works
The pourpose is to use every symbol ,number,letters only once
and we have to be sure to use them always together when the requested psw>= 7 chars
Code:
set<int> getAscii(int index );
void printAscii(set<int>);
const int nChar = 7;
int main()
{
set<int> psw;
std::cout << "Hello World!\n\n";
srand(time(NULL)); // seed
/* Gets n printable chars througth 7 symbol group */
/* pick a random symbol for every group*/
/* every symbol will be present only if nchar >=7*/
psw = getAscii(nChar); // Prints n chars and replaces eventual duplicated number
printAscii(psw);
}
set<int> getAscii(int howManyTimes)
{
const int end = 1;
const int start = 0;
set<int> psw;
set<int>::iterator it;
int ascii[7][2] = {
{97, 122}, /* Lower case*/
{48, 57} , /* Numbers */
{32, 47} , /* Symbols 1*/
{58, 64} , /* Symbols 2*/
{91, 96} , /* Symbols 3*/
{123, 127} ,/* Symbols 4*/
{65, 90} , /* upper case*/
};
int rnd = 0;
int index = 0;
int first = 0;
int second = 0;
bool success;
/* Pick x number and swaps ascii[][] x times( a kind of "shuffle" )*/
int times = rand() % 32 +1;
/* swap ascii[][] elements by random position */
for (int sw = 0; sw < times; ++sw) {
first = rand() % 7;
second = rand() % 7;
swap(ascii[first][start], ascii[second][start]);
swap(ascii[first][end], ascii[second][end]);
}
/* Generates random psw 'howManyTimes' times */
for (int times = 0; times < howManyTimes; times++) {
do { // If insert fails (duplicate) , retry to gets a new number
rnd = rand() % (ascii[index][end] - ascii[index][start]) + ascii[index][start];
success = psw.insert(rnd).second;
} while (!success);
index = (index + 1) % 7; // ready to pick number fron next ascii group
}
return psw;
}
void printAscii(set<int> psw)
{
set<int>::iterator it;
setlocale(LC_ALL, ".437");
cout << "PSW:";
for (it = psw.begin(); it != psw.end(); it++) {
cout << (char)*it;
}
cout << " Size:" << psw.size() << endl;
}