#include <iostream>
#include <string>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
usingnamespace std;
int main()
{
srand(time(0)); // seed random number generator
string sString; // length 0
sString.reserve(64); // reserve 64 characters
// Fill string up with random lower case characters
for (int nCount=0; nCount < 64; ++nCount)
sString += 'a' + rand() % 26;
cout << sString;
}
I'm confused about this part right here:
'a' + rand() % 26
The rand() function returns an integral number in the range between 0 and RAND_MAX. I assume RAND_MAX here is 26. And this is randomly return one of the 26 letters of the alphabet. But what is the point of the 'a' here?
If I want a random number between 5 and 10, then that means that 5 is the minimum, and 10 the maximum.
Therefore:
5 + (rand()%10)
In ascii, 'a' is 97 in decimal.
The purpose of the 'a' in this case, is to create a random number and then add 'a'(97), the minimum, to it. Otherwise, you'd be creating characters from 0-25