I need a random function for number betwen 11 and 99. I wrote this:
int random (void){
int i2;
i2=11+(rand()%99);
return i2;
}
but the numbers go over 99. Why?
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.
Because if
rand()return98, then98 % 99is98and98 + 11 > 99.To do this, you need
rand() % 89will give you numbers[0, 88], so+11would become[11, 99].By the way, don’t forget to
srand( time( NULL ) ), otherwise it will (most probably) generate the same sequence of (pseudo) random numbers all the time.