how does a rand() works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how does a rand() works?

#include <iostream> #include <cstdlib> using namespace std; int main() { for (int x = 1; x <= 10; x++) { cout << rand() << endl; } } every time how it's output can be same?

25th Sep 2017, 2:39 PM
Shivani Goyal
1 Answer
+ 5
you'll need to use a seed like this: #include <ctime> srand(time(nullptr)); so the current time is the seed for the rng now
25th Sep 2017, 3:12 PM
johan beimers
johan beimers - avatar