Need help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help.

All the time this code is show the same random number. What's the problem of this code? https://code.sololearn.com/ckMOa8m0s7na/?ref=app

29th Oct 2020, 5:44 PM
Akbar Hasan
Akbar Hasan - avatar
1 Answer
+ 2
It is by design that rand() returns a consistent sequence of numbers so that you can get consistent results for testing. When you are ready to release the code, you can make it more unpredictable by initializing the sequence with a randomizing seed value. This is done by calling srand(). Commonly, the seed value is taken from the current time. #include <time.h> srand (time(NULL)); You need to call srand() only once at the beginning of your program. Thereafter, use rand() to provide all the pseudorandom numbers you want.
29th Oct 2020, 7:14 PM
Brian
Brian - avatar