Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Algorithms ascii
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:
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 5-Nov-12 8:01am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

OK - look at it in parts:
        srand((unsigned) time(NULL));
Initialises a random number generator.
        const int passLen = 10;
        for (int i = 0; i < passLen; i++) {
Makes the code generate 10 characters
                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.
  Permalink  
Comments
Parwarrior7 at 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 at 5-Nov-12 14:53pm
   
I would setup two functions - it would make the code that is calling them clearer.
CPallini at 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 at 5-Nov-12 15:49pm
   
:thumbsup: This results in much simpler and readable/maintainable code. Also offers more flexibility regarding the charset.
Maciej Los at 5-Nov-12 14:49pm
   
Short and to the point, a 5!
Parwarrior7 at 5-Nov-12 14:55pm
   
Great, Thanks for the help, def a 5.
ridoy at 5-Nov-12 15:19pm
   
good explanation..+5
CPallini at 5-Nov-12 15:45pm
   
5.
pasztorpisti at 5-Nov-12 15:50pm
   
+5

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100