I am trying to convert an int
to a char
using the ASCII value. The int
is randomly generated between 97
and 122
(from a
to z
).
How can I generate a random number between 97 and 122 and convert it to a char?
I searched many answers before asking my question, but none solved the problem or was completely related to my need. Even this didn't work.
Here is what I'm trying to do: in a for loop, the program generates a random number and converts it to an int
. This is converted to its ASCII value and then placed into a QString
. When the for loop is finished, I send the QString
to a line_edit
.
By following the above link, I only got an m
.
Here's my code:
QString str;
int maxlenght = 16; //the user will be able to set himself the maxlenght
for (int i =0; i<maxlenght; i++)
{
int random = 97 + (rand() % (int)122-97+1);
char letter = (char)random;
if(i > 0)
{
str[i] = letter;
}
}
And though the random number is generated in the loop, it always gives me the same char.