How it's random if it generates the same numbers each time i run the function?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How it's random if it generates the same numbers each time i run the function??

i need an answer plz How it's random if it generates the same numbers each time i run the function??

4th Mar 2017, 3:57 PM
ihab ouda
ihab ouda - avatar
4 Answers
+ 8
add srand(time(0)); to the start of main function and you also need to add #include <ctime>
4th Mar 2017, 4:17 PM
Burey
Burey - avatar
+ 6
You need a random function based on time. Set a seed based on the current time to generate a random number based off of a different seed every time.
4th Mar 2017, 3:58 PM
J.G.
J.G. - avatar
+ 3
because, the rand() function is not truly random. if u want to generate truly random number I suggest you use srand(time(0)); and don't forget to include <ctime>
4th Mar 2017, 4:48 PM
Eyuel Solomon
Eyuel Solomon - avatar
+ 2
like they mentioned above, you can use srand(time(0)). in addtion , if you use visual studio as your compiler,you can use srand(time(nullptr)) as well.
5th Mar 2017, 2:09 AM
Steven Cai
Steven Cai - avatar