using random to generate strings..

thundemanx (2)
Now ,Random()[or rand()] is ,in my knowledge generally and only used to get random integers..

Suppose, I want to generate a random 3-character string that may contain
a-z
OR
A-Z

How to do this ?
Besides using random , is there any other way to do this ?
Catfish3 (117)
If your compiler supports C++11 well enough...
http://cplusplus.com/reference/random/
http://cplusplus.com/reference/algorithm/generate/
EssGeEich (683)
If it doesn't, get a random number between 'a' and 'z' or 'A' and 'Z'.
1
2
3
4
5
6
7
8
if(rand() % 2) // Generate a value. Depending on it...
{
    char ThisChar = (rand()%('Z'-'A')) + 'A'; // Generate a uppercase one
}
else // or...
{
    char ThisChar = (rand()%('z'-'a')) + 'a'; // Generate a lowercase one
}
tntxtnt (61)
1
2
3
4
5
6
7
8
string rand3LetterStr()
{
  char temp[4];
  for (int i = 0; i < 3; ++i)
    temp[i] = 65 + rand() % 26 + 32 * (rand() % 2);
  temp[3] = '\0';
  return temp;
}
Registered users can post here. Sign in or register to post.