Hello all!
It's Tech2011 again!
I have another stupid question to throw out there for you all!
I am looking for an algorithm I could use in C++ to develop random strings of both alphabetical letters, ASCII symbols, and integers.
I have developed an algorithm that does this sort of thing, but not quite random...only partially random. Here's what I've developed:
srand((unsigned)time(0));
int bit=rand() % 9 + 1;
cout << "Random string: ";
switch (bit){
case 1:
cout << "S" << rand() % 99999 << "/" << rand() % 999999 <<"A~D" << "\n" ;
break;
case 2:
cout << "PoP" << rand() % 99999 << "7(Y" << rand() % 99999 << "\n" ;
break;
case 3:
cout << rand() % 99999 << "Tk-i" << rand() % 99999<< "\n" ;
break;
case 4:
cout << "&" << rand() % 99999 << "V" << rand() % 99999 <<"#W"<<"\n" ;
break;
case 5:
cout << "@R" << rand() % 99999 << rand() % 99999 <<"yK"<< "\n" ;
break;
case 6:
cout << rand() % 99999 << "PhP/d" << rand() % 99999<< "\n" ;
break;
case 7:
cout << rand() % 99999 <<"J" << rand() % 99999 <<"Fl"<< "\n" ;
break;
case 8:
cout << rand() % 99999 << ";" << rand() % 99999 <<"W!-]" << "\n" ;
break;
case 9:
cout << "*Q" << rand() % 99999 << "mR=" << rand() % 99999 << "\n" ;
break;
default:
cout << "String could not be generated because the default switch was reached." <<bit;
break;
Of course, there are multiple problems with this. For one thing, the majority, or at least a good portion of the "random" string is predictable and is reproducible. So it really isn't random, since the rand() function I've used, even with the time seeded into the algorithm, is still predictable. It's obvious that I could just change the letters and symbols inputted in the switch statement every so often, but that would not be efficient and would only be partially random and would eventually become reproducible. And, I know that I could/should add more switch statements with more random letters and symbols, but again, one of my goals is efficiency, alongside randomness. I understand that C++ rand() function was not designed to generate really good randomness, but I should at least be able to generate better randomness than what is provided in the algorithm I've developed.
Any and all suggestions to improve this algorithm, or a new algorithm, are welcome and appreciated!
Thanks again and sorry if this seems like a stupid question!
Tech2011