why this code is giving me 41 as output only if this function is generating random numbers than it should give me different values every time I run it . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why this code is giving me 41 as output only if this function is generating random numbers than it should give me different values every time I run it .

rand only generating value as 41

11th Oct 2016, 7:04 AM
Samarth Madan
Samarth Madan - avatar
1 Answer
+ 4
rand does some calculations starting from a "seed". If the seed is the same, the result of rand is the same. You have to change the seed every time to have different numbers. Using: #include <cstdlib> #include <ctime> srand(time(NULL)); a=rand(); you use actual time (that change continuously) as seed, so the result of rand will be different every time.
11th Oct 2016, 7:45 AM
marcram