srand() and If-statements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

srand() and If-statements

How do I generate a number when I have multiple If-statements? Where should I place the srand() function, before or after conditions? And what else do I need to add to the code

27th Nov 2016, 12:21 PM
Julia Melnik
Julia Melnik - avatar
3 Answers
+ 4
To generated random number you need to use rand() function and srand() function use to initialization of rand. Put your srand above if statement. And initialize rand by srand(time(NULL)). You have to use ctime header file for time function. it would work perfect. if you want it within a range use rand()%(max-min+1)+min
27th Nov 2016, 12:28 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
Helo
29th Mar 2020, 12:30 PM
RAJESH PERAM
RAJESH PERAM - avatar
0
yes
7th Oct 2021, 3:13 PM
Sete Csongor Krisztián
Sete Csongor Krisztián - avatar