I have found this line of code in a game that I study
int charaCode = arc4random() % (126-'!'+1)+'!';
I know what arc4random is but the expression is strange to me.
What is the purpose of
(126-'!'+1)+'!'
It always evaluates to 127.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You interpreted it wrong: the
%operator has a higher precedence than+.So, in effect, you have:
which clips the function result to
0..93and shifts it so that it starts with'!'.So the effective range of what you get is
33..126(which is the range of all visible ASCII characters from!to~).