+ 1

Random number in c

Will the generated random number have different value when i run it again and again? #include <stdio.h> #include <stdlib.h> int main() { unsigned short int random =88; int i; for(i=0;;i++) { random = rand(); printf("The random number generated %d\n",random); if(random <= 100 && random >=1) { printf("Random number is between 1 to 100\n"); break; } } printf("Random no: %d",random); return 0; } in the above case?

17th Oct 2020, 4:06 AM
Srujan Landeri
Srujan Landeri - avatar
4 Answers
+ 4
No, you need to modify the seed. The common way to use it is srand(time(0)), don't forget to include time header file. Btw, what's that 88 for?
17th Oct 2020, 4:16 AM
LastSecond959
LastSecond959 - avatar
+ 2
Okay thank you..😄
17th Oct 2020, 4:25 AM
Srujan Landeri
Srujan Landeri - avatar
+ 1
I wrote 88 because I thought that variable might take a garbage value if not initialized(as it is a local variable) ..so I wouldn't know weather the value in it is garbage value or a random number .. and thanq for the answer
17th Oct 2020, 4:21 AM
Srujan Landeri
Srujan Landeri - avatar
+ 1
Srujan Landeri It won't take a garbage value since you replaced it with rand().
17th Oct 2020, 4:22 AM
LastSecond959
LastSecond959 - avatar