12,304,566 members (64,108 online)
Rate this:
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:
```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

Rate this:

## 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.
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!
Parwarrior7 5-Nov-12 14:55pm

Great, Thanks for the help, def a 5.
ridoy 5-Nov-12 15:19pm

good explanation..+5
CPallini 5-Nov-12 15:45pm

5.
pasztorpisti 5-Nov-12 15:50pm

+5

Top Experts
Last 24hrsThis month
 Sergey Alexandrovich Kryukov 330 OriginalGriff 281 Karthik Bangalore 175 Peter Leow 144 BillWoodruff 135
 OriginalGriff 10,327 ppolymorphe 5,966 CHill60 5,867 Karthik Bangalore 5,566 Richard MacCutchan 5,357