+ 1

Why each time I run this code it's the same result using rand() function?

https://code.sololearn.com/cwDgi70MrLjt/?ref=app

17th Sep 2019, 7:22 AM
Hadjer
Hadjer - avatar
3 Respostas
+ 3
You need to set a seed first. Otherwise the random number generator will start from the same value. https://www.tutorialspoint.com/c_standard_library/c_function_srand.htm
17th Sep 2019, 7:27 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 7
Use time.h header and srand to get random values in everytime.
17th Sep 2019, 7:53 AM
Aung Thiha
Aung Thiha - avatar
+ 2
#include <stdio.h> #include <time.h> int main() {int b; srand(time(NULL)); for(int i=0;i<10;i++){ b= rand()%5; printf ("%d\n",b); }; return 0; }
17th Sep 2019, 1:52 PM
✳AsterisK✳
✳AsterisK✳ - avatar