Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone,

In my program I have to generate a random number/capital letter and cast it as a char.
Online I was able to locate an algorithm that does nearly that:
C++
int main() {
        srand((unsigned) time(NULL));
        
        const int passLen = 10;
        for (int i = 0; i < passLen; i++) {
                cout << (char) (rand() % ('z' - '0' + 1) + '0');
        }
}


My problem is, I don't completely understand how this method works, and because of this I'm not sure how to modify the function to get it to do what I want. Could anyone please explain approximately how this process works so that I can learn to modify this function to my liking?

Thank you, sorry for the fairly basic question. I'm still pretty new to this.
Posted

1 solution

OK - look at it in parts:
C++
srand((unsigned) time(NULL));
Initialises a random number generator.
C++
const int passLen = 10;
for (int i = 0; i < passLen; i++) {
Makes the code generate 10 characters
C++
cout << (char) (rand() % ('z' - '0' + 1) + '0');
Does teh actual work.
The cout stuff I assume you understand.
rand() generates a new random number.
% Modulus operator - effectively limits the random number to between zero and the second parameter minus one.
'z' - '0' + 1 The second parameter, sets upper limit of the range of characters.
+ '0' Converts the result to a printable ASCII character - it the random number is 0, then '0', if it is one, the '1', if it is nine then '9', if it is ten then ':' - see here http://www.asciitable.com/[^] for the Ascii characters and you will see what I mean.

The second parameter is just saying "I want my results to be between '0' and 'z', so give me value that is the difference plus one" - because the one will be discarded by the modulus operation. if it was '0' to '9' then it would be 57 - 48 + 1 which is (obviously) 10 - so teh random number would be 0 to 9 inclusive.
 
Share this answer
 
Comments
Parwarrior7 5-Nov-12 14:48pm    
Very informative thank you :), So I see now how I can set up my lower and upper bounds properly. Is it somehow possible to also omit the mid range section of characters(:,;,< etc.) between the numbers and capital letters within one function, or must I set up two different functions with different upper and lower bounds, one for numbers and one for letters respectively?
OriginalGriff 5-Nov-12 14:53pm    
I would setup two functions - it would make the code that is calling them clearer.
CPallini 5-Nov-12 15:44pm    
As an alternative you may explicitely initialize one array of char with the allowed letters, say:
char allowed[]={'0', '1', .., '9', 'A', 'B' ,.., 'Z'};
and then randomly choose the index of the array:
index = rand() % sizeof(allowed);
pasztorpisti 5-Nov-12 15:49pm    
:thumbsup: This results in much simpler and readable/maintainable code. Also offers more flexibility regarding the charset.
Maciej Los 5-Nov-12 14:49pm    
Short and to the point, a 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900