Why is the result here constantly 41 if it is meant to produce random numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why is the result here constantly 41 if it is meant to produce random numbers?

#include <iostream> #include <cstdlib> using namespace std; int main() { cout << rand(); // Outputs 16 return 0; }

21st Jan 2018, 3:28 PM
Eric Baraza
Eric Baraza - avatar
3 Answers
+ 3
Add these: #include <ctime> Before use rand: srand(time(0)); No idea why, random numbers are annoying in C++
21st Jan 2018, 3:31 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 3
this code with some comment might help a little, srry no comments were there but basically I m using input age as a seed value to produce a specific random no. for that age https://code.sololearn.com/cYh9VfKXfbfC/?ref=app
21st Jan 2018, 5:27 PM
Morpheus
Morpheus - avatar
+ 1
That gave random numbers, thanks. Is this using time to randomize?
21st Jan 2018, 3:35 PM
Eric Baraza
Eric Baraza - avatar