# Possible algorithms for random strings in C++

This is a discussion on Possible algorithms for random strings in C++ within the C++ Programming forums, part of the General Programming Boards category; Hello all! It's Tech2011 again! I have another stupid question to throw out there for you all! I am looking ...

1. ## Possible algorithms for random strings in C++

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

2. Well you could post your code with the all-important braces in place, rather than ripping them all out so you don't have to deal with the code tags issue.

You know, it's there for a reason - to make code readable.

[code]
Code:
```int main ( ) {
// your code here, nicely indented
return 0;
}```
[/code]

3. Here's a start:

Code:
```#include <string>
#include <cstdlib>
#include <ctime>

class random_text_generator
{
public:
random_text_generator(const std::string& str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
: m_str(str)
{
std::srand(std::time(0));
}
std::string operator ()(std::size_t len = 1)
{
std::string seq;
std::size_t siz = m_str.size();
if(siz)
while(len--)
seq.push_back(m_str[rand() % siz]);
return seq;
}
private:
std::string m_str;
};

#include <iostream>

int main(void)
{
using namespace std;
random_text_generator rtg;
for(size_t cnt = 0; cnt < 16; ++cnt)
{
cout << rtg(cnt) << endl;
}
}```

4. How does showing a possible solution help the OP in any way? Stop handing out answers and let people think for themselves.

5. Originally Posted by VirtualAce
How does showing a possible solution help the OP in any way?
Providing a straight-forward, concrete example is sometimes the best approach, especially when someone seems quite far from understanding a given problem. Notice that the OP was not whining for help, and moreover that my response was not exactly a solution to his particular problem. It was just a helpful nudge in the right direction.

6. @gardhr, you forgot to put "return 0;" at the end of main!

7. Originally Posted by Sipher
@gardhr, you forgot to put "return 0;" at the end of main!
Ah, this is a style point in C++; the language allows for an implicit return. See Stroustrup FAQ-2

8. Yeah, so I see. But I prefer it to be there.

9. Originally Posted by Sipher
Yeah, so I see. But I prefer it to be there.
As do I, my friend.

10. Originally Posted by gardhr
Providing a straight-forward, concrete example is sometimes the best approach, especially when someone seems quite far from understanding a given problem. Notice that the OP was not whining for help, and moreover that my response was not exactly a solution to his particular problem. It was just a helpful nudge in the right direction.
And you have to know that he just did the scoop and poop, dropping it directly into his own code, then automatically turned off his brain so that he doesn't have to waste any more time thinking about stupid things like *his homework*.

11. Originally Posted by CommonTater
And you have to know that he just did the scoop and poop, dropping it directly into his own code, then automatically turned off his brain so that he doesn't have to waste any more time thinking about stupid things like *his homework*.
Or maybe it stimulated a creative thought process? Teaching isn't all about forcing people to figure everything out on their own. Demonstrating practical techniques helps to hone problem-solving skills, and tends to reinvigorate interest as well.

My impression of the OP was that he was earnestly seeking some guidance, so I chose to post a working example. That's all.

12. Originally Posted by CommonTater
And you have to know that he just did the scoop and poop, dropping it directly into his own code, then automatically turned off his brain so that he doesn't have to waste any more time thinking about stupid things like *his homework*.
As much as I don't like solution dumping either, we don't know this is homework. I get the impression that it isn't, but if it is then clearly nobody is going to believe that he wrote that anyway, and for most assignments we see here they are not allowed to use the SC++L. Having a look at the other thread the OP started, he seems like an honest person, using his real name and all.
If it had been preceeded by an explanation of a how one could go about designing such a thing, then I think it would be fine.

13. Umm, guys, I don't think any of us answered the OP's question. ( I know I didn't! )

He asked for a better random numbers algorithm, one which isn't predictable.

I suggest doing a little search on the web about RNG ( Random Number Generator ) and PRNG ( Pseudo RNG ) for details. Wikipedia was some good articles!

14. > I have developed an algorithm that does this sort of thing, but not quite random...only partially random. Here's what I've developed:

And is this line
srand((unsigned)time(0));

In the same kind of loop or function being used to call rand() ?

Well it shouldn't be.

Put
srand((unsigned)time(0));
right at the start of main(), and make sure it's only done ONCE!

The standard rand() is good enough for any likely homework scenario - if used correctly.

Popular pages Recent additions